Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best implememtation of react shouldComponentUpdate with immutable.js

I'm new to ImmutableJS. My app implements large Redux Store & multiple react components.

Correct me if I'm wrong:

  • I understand that the benefits of Immutable is to protect Flux Store and to avoid unnecessary vDom rendering on component getting unchanged props.
  • To benefit from better rendering performance with ImmutableJS, 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:

  • Facebook implements shallowEqual for React and more I imagine.
  • Jurassix offers an implementation that implements shallowEqualImmutable. It the function from Facebook except that the is() function is replaced by the one given by ImmutableJS. The first equality is different too.
  • Dan does the same thing with a different shalllowEqual function that implement parts of the two previous implementation.

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!!

like image 369
Damien Leroux Avatar asked Apr 14 '16 17:04

Damien Leroux


People also ask

Why use immutable JS react?

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.

What is immutable JS react?

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.

Are react components immutable?

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.

What is true about immutability in React state update?

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.


1 Answers

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.

like image 81
Dan Abramov Avatar answered Sep 30 '22 17:09

Dan Abramov