Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce renders using redux + normalizr

I have an app using React + Redux + Normalizr, I would like to know the best practices to reduce the number of renders when something changes on entities.

Right if I change just one entity inside the entities, it will re-render all the components and not only the components that need that specific entity

like image 948
Sibelius Seraphini Avatar asked Apr 03 '16 22:04

Sibelius Seraphini


2 Answers

There are several things you can do to minimize the number of renders in your app, and make updates faster. Let's look at them from your Redux store down to your React components.

  • In your Redux store, you can use immutable data structures (for example, Immutable.js). This will make all subsequent optimizations faster, as you'll be able to compare changes by only checking for previous/next state slice equality rather than recursively comparing all props.

  • In your containers, that is in your top-level components where you inject redux state as props, ask only for the state slices you need, and use the pure option (I assume you're using react-redux) to make sure your container will be re-rendered only if the state slices returned by your mapStateToProps functions have changed.

  • If you need to compute derived data, that is if you inject in your containers data computed from various state slices, use a memoized function to make sure the computation is not triggered again if the input doesn't change, and to keep object equality with the value the previous call returned. Reselect is a very good library to do that.

  • In your dumb components use the shouldComponentUpdate lifecycle to avoid a re-render when incoming props do not change. If you do not want to implement this manually, you can use React's PureRenderMixin to check all props for you, or, for example, the pure function from the Recompose library if you need more control. A good use case at this level is rendering a list of items. If your item component implements shouldComponentUpdate only the modified items will be re-rendered. But this shouldn't be a fix-all-problems habit : a good components separation is often preferable in that it makes flow props only to those components which will need them.

As far as Normalizr is concerned, there is nothing more specific to be done.

like image 62
VonD Avatar answered Nov 15 '22 20:11

VonD


If in some case (it should be rare) you detect performance problems that are directly related to React's rendering cycles of components, then you should implement the shouldComponentUpdate() method in the involved components (details can be found in React's docs here).

Change-detection in shouldComponentUpdate() will be particularly easy because Redux forces you to implement immutable state:

shouldComponentUpdate(nextProps, nextState) {

    return nextProps.dataObject !== this.props.dataObject;

    // true when dataObject has become a new object,
    // which happens if (and only if) its data has changed,
    // thanks to immutability
}
like image 34
pierrepinard_2 Avatar answered Nov 15 '22 21:11

pierrepinard_2