I'm new to ImmutableJS. My app implements large Redux Store & multiple react components.
Correct me if I'm wrong:
shouldComponentUpdate()
must be implemented.What is the best implementation of this function?
I already found several implementations of it, all using shallowEqual() with some modifications:
is()
function is replaced by the one given by ImmutableJS. The first equality is different too.Someone knows which implementation I should use in my case? or none and implement specific shouldComponentUpdate()
? I am slightly at a loss on this point
Thank you a lot for any help!!
Immutable. js allows us to detect changes in JavaScript objects/arrays without resorting to the inefficiencies of deep equality checks, which in turn allows React to avoid expensive re-render operations when they are not required. This means Immutable. js performance tends to be good in most scenarios.
Immutable. js is a library that supports an immutable data structure. It means that once created data cannot be changed. It makes maintaining immutable data structures easier and more efficient.
Let's come to the main question “Are React props immutable?” No, it's not completely true. React props are shallow immutable. It will change the original propertyName in the Parent component also.
An immutable value or object cannot be changed, so every update creates new value, leaving the old one untouched. For example, if your application state is immutable, you can save all the states objects in a single store to easily implement undo/redo functionality.
I understand that the benefits of Immutable is to protect Flux Store and to avoid unnecessary vDom rendering on component getting unchanged props.
This is not really related to Immutable (if you mean the library). For example, you can use plain objects and arrays with Redux but since Redux asks you to never mutate them, you get pretty much the same benefits in most cases. So Immutable library can offer a nicer API for updating things immutably, but it is not required for performance optimizations if you don’t mutate plain objects or arrays.
To benefit from better rendering performance with ImmutableJS, shouldComponentUpdate() must be implemented.
Again, not really related to ImmutableJS, but yes, to benefit from immutability in props, you’d need to implement shouldComponentUpdate()
. However if you use Redux you probably already use connect()
from React Redux package which implements shouldComponentUpdate()
for you for most cases. So you don’t really need to write it by hand for any connect()
ed components.
Someone knows which implementation I should use in my case? or none and implement specific shouldComponentUpdate()? I am slightly at a loss on this point
If you don’t have performance problems, don’t use either. React by itself is fairly performant in most cases, and a connect()
on top of it will add a good default implementation of shouldComponentUpdate()
.
For components that are not connect()
ed but still get frequently updated, I would suggest you to use react-addons-shallow-compare
. It is used by PureRenderMixin
internally but since mixins are not really used in modern React APIs, a separate function can be more convenient.
If you want special support for Immutable.is
, you can indeed use something like shallowEqualImmutable
. It understands Immutable collections better, as it considers lists of the same values to be the same. At this point you would be better off profiling different implementations against your app, as the specifics can vary depending on your use case.
Don’t optimize prematurely, make sure this is an actual problem before solving it.
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