I have built my first React application with stateful stores the "normal" way, and now I am looking into using an immutable global state like used in the Este starterkit.
It simplifies the application structure in a few ways:
I only read positive things about using immutable data with React, and it is recommended to avoid state in components, so I wonder if there are any disadvantages. I figured there must be because otherwise I don't see why it isn't recommended as the way to structure React apps.
Immutability is new to me, so are there any caveats I should be aware of if I start using this approach in a complex real world app?
The only minor thing I can think of is the use of forceUpdate() as Este is using it, as I've read that it is a synchronous function. Morearty for example seems to defer the updates to the next animation frame in order to batch them, but I consider this an implementation detail / optimization and not some inherit downside of the immutable single state approach.
Besides reduced memory usage, immutability allows you to optimize your application by making use of reference- and value equality. This makes it really easy to see if anything has changed. For example a state change in a react component.
One of the advantages of immutability is that you can optimize your application by making use of reference and value equality. This makes it easy to identify if anything has changed. You can consider the example of state change in the React component.
Using immutable states allows us to write code that can quickly tell if the state has changed, without needing to do a recursive comparison on the data, which is usually much, much faster.
this.state.getIn("[parent, child, index]")
in a component because it increases the possibility of a model change breaking the component code. This is preventable by extensibility (more on that below) or through helper methods, but you certainly lose the simplicity of plain JS members.One advantage of passing data through props is that you can utilize shouldComponentUpdate
for more performant updates. If you assume data always comes from the parent (e.g. via props), you can effectively prevent a large portion of your component tree from having to rerender. Immutable values make this very nice as you can do reference equality checks in shouldComponentUpdate
relatively high in your component hierarchy.
The only other disadvantage I've found when using immutable data with React is that the React developer tools don't work super well with them (they show you the immutable values' underlying data structure instead of a JS-friendly value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With