Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make props immutable to prevent rerender in React?

I have been creating a small app using react.js. I take 'performance' into account excessively.

So I have a simple child component named "Spinner". My goal is to make sure this component never re-renders.

Here is my component:

import React, {PureComponent} from 'react';

export default class Spinner extends PureComponent {

render() {
    return (
        <div className="spinner">
            <div className="bounce1"></div>
            <div className="bounce2"></div>
            <div className="bounce3"></div>
        </div>
    )
}

}

In the time of the re-rendering with 'react-addons-perf', the component is always rendering, I am using PureComponent because I want that component to render only one time, I read that I can use immutable props but I don't know how to make this possible.

If I make some like to this:

componentDidMount() {
    this.renderState = false;
}

shouldComponentUpdate(nextProps, nextState) {
    return (this.renderState === undefined) ? true : this.renderState;
}

It is rendering only one time, but I believe that there is a better way.

How do I avoid the re-render? or maybe How I can make a immutable props?

like image 750
Jedidias Avatar asked Dec 09 '16 23:12

Jedidias


People also ask

How props are immutable in React?

Props are immutable, while state is mutable Although a component is not allowed to change its props, it is responsible for the props of its child components down the component tree. On the other hand, state is mutable. A stateful component changes its state every time users interact with the app.

How do you prevent re-render in React?

1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With this technique, developers can avoid unnecessary renderings and reduce the computational load in applications.

Does changing props cause re-render?

⛔️ Re-renders reason: props changes (the big myth) In order for props to change, they need to be updated by the parent component. This means the parent would have to re-render, which will trigger re-render of the child component regardless of its props.

How do you prevent re-rendering of components that have not changed?

To prevent excessive re-rendering, move the expensive component to a parent component, where it will render less often, and then pass it down as a prop. If this is not enough, inject React. memo into the code for better performance.


2 Answers

You don't need an extra logic for componentShouldUpdate, since you don't want your component to rerender ever.

Adding only this should be enough to prevent component to rerender:

shouldComponentUpdate(nextProps, nextState) {
    return false
}
like image 200
FurkanO Avatar answered Nov 14 '22 23:11

FurkanO


For stateless components that don't need props, we can use a combination of a Functional (stateless) component, and babel's React constant elements transformer to optimize the component creation, and prevent rerenders entirely.

  1. Add React constant elements transformer to your build system. According to the docs the transformer will:

    Hoists element creation to the top level for subtrees that are fully static, which reduces call to React.createElement and the resulting allocations. More importantly, it tells React that the subtree hasn’t changed so React can completely skip it when reconciling.

  2. Change the spinner to a stateless component.

    const Spinner = () => (
      <div className="spinner">
        <div className="bounce1"></div>
        <div className="bounce2"></div>
        <div className="bounce3"></div>
      </div>
    );
    
    export default Spinner;
    
like image 33
Ori Drori Avatar answered Nov 15 '22 00:11

Ori Drori