question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The state should be immutable

See original GitHub issue

Current state

Since the addition of modules, the state is no longer immutable: when an action, or a nested action, or an action in a module is called, its result is merged with the current state (or state slice for nested actions/modules).

This prevent users from using memoize functions on the view with simple === checks for equality checks and forces to use deep equality.

For example, the following pen should work with immutable state: https://codepen.io/mytrill/pen/WZmVWQ?editors=0010

I believe for large apps, the benefit of having very cheap memoize functions based on immutable state far outweight the cost of maintaining an immutable state (both in terms of performance and source code size).

Expected behavior

When an action gets executed, the result that it returns should get merged to the state slice/module’s state this action operates on and create a new instance of the state and re-use the part of the state that hasn’t been touched. Here is an example: https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Purely_functional_tree_after.svg/438px-Purely_functional_tree_after.svg.png

In this case, when the node “f” is modified (“e” gets added to it) the update function recursively goes up the state tree, creating new nodes when modified and re-using un-touched nodes.

Advantages

  • Allow debug tools to keep the state history and implement time travel
  • Allow very performant memoization of components/views on large app (deep equality on state can be checked with ===)

Drawbacks

  • small performance penalty when executing nested action (need to duplicate a whole branch of the state-tree
  • extra logic/bundle size

Implementation

PR #426 addresses this issue

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Oct 21, 2017

@lukejacksonn BTW, I need more time before merging #424, since I am not sure it interferes with making the state immutable again.

0reactions
lukejacksonncommented, Oct 21, 2017

Immutable guarantees, cheap memoizing, negligible perf hit and somewhat of an existing implementation… for ~100b you say 🤔 sounds like good deal to me 💸

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is redux state immutable? - SuperTokens
For redux to work correctly, the state must be immutable. This means that whenever we update the redux state, we have to create...
Read more >
The State of Immutability - Medium
Object without state or object with immutable state gives guarantee and trust for all parties consuming it. Immutable objects are amplifying single ...
Read more >
Why should objects in Redux be immutable? - Stack Overflow
To sum up another way, immutability is a requirement of Redux because Redux represents your application state as "frozen object snapshots". With these...
Read more >
Redux FAQ: Immutable Data
Immutability can bring increased performance to your app, and leads to simpler programming and debugging, as data that never changes is easier ...
Read more >
Immutability in JavaScript Using Redux - Toptal
Using immutable states allows us to write code that can quickly tell if the state has changed, without needing to do a recursive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found