Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactjs : ShouldComponentUpdate for states

Tags:

reactjs

How can I use shouldComponentUpdate for states?

I can check:

shouldComponentUpdate(nextProps, nextState) {   return this.state.value != nextState.value; } 

But it doesn't have any sense for states. Because if I change state(this.setState({value: 'newValue'})) this.state will be equal nextState.

For example, onClick event:

handleClick() {   this.setState({value: 'newValue'}); } 
like image 245
Nick Avatar asked Oct 07 '16 23:10

Nick


People also ask

What does this state contain in shouldComponentUpdate method?

The shouldComponentUpdate method allows us to exit the complex react update life cycle to avoid calling it again and again on every re-render. It only updates the component if the props passed to it changes.

When should I use shouldComponentUpdate?

Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.

Does React re-render if state changes?

As we already saw before, React re-renders a component when you call the setState function to change the state (or the provided function from the useState hook in function components). As a result, the child components only update when the parent component's state changes with one of those functions.

Does ComponentDidUpdate run on state change?

ComponentDidUpdate() is invoked immediately after updating occurs and is not called for the initial render. The most common use-case for the componentDidUpdate () lifecycle method is updating the DOM in response to Prop or State changes.

What is the use of shouldcomponentupdate () method in react?

The shouldComponentUpdate (nextProps, nextState) method works for both props and state. In your example, after the onClick event the following method is fired by React. The key here is that in the above method the value of this.state.value is equal to what it was before the setState () call.

How to update the state of a React component?

the state is updated by a call to setState () (the only way allowed by react). This includes all scenario's where the value of state is exactly the same. Sometimes it can be useful to let the re-render cycle go through, even though the new value is exactly the same as the old value.

What happens when forceupdate is called in react?

Calling forceUpdate() will cause render() to be called on the component, skipping shouldComponentUpdate(). This will trigger the normal lifecycle methods for child components, including the shouldComponentUpdate() method of each child. React will still only update the DOM if the markup changes.

Does shouldcomponentupdate work for both props and state?

The shouldComponentUpdate (nextProps, nextState) method works for both props and state. In your example, after the onClick event the following method is fired by React.


1 Answers

The shouldComponentUpdate(nextProps, nextState) method works for both props and state. In your example, after the onClick event the following method is fired by React.

shouldComponentUpdate(nextProps, nextState) {   return this.state.value != nextState.value; } 

The key here is that in the above method the value of this.state.value is equal to what it was before the setState() call. This is thanks to the fact that in React:

setState() does not immediately mutate this.state but creates a pending state transition.
React docs: https://facebook.github.io/react/docs/component-api.html#setstate

Have a look at this demo: http://codepen.io/PiotrBerebecki/pen/YGZgom (full code below)

React keeps on state the count of every click on the button and also saves the randomly picked value (true or false). However thanks to the shouldComponentUpdate method, the component is re-rendered only if the previous value is not equal to upcoming / new value. This is why the displayed click count sometimes skips rendering its current state. You can comment out the whole shouldComponentUpdate method to re-render on every click.

class App extends React.Component {   constructor() {     super();     this.state = {       value: true,       countOfClicks: 0     };     this.pickRandom = this.pickRandom.bind(this);   }    pickRandom() {     this.setState({       value: Math.random() > 0.5, // randomly picks true or false       countOfClicks: this.state.countOfClicks + 1     });   }    // comment out the below to re-render on every click   shouldComponentUpdate(nextProps, nextState) {     return this.state.value != nextState.value;   }    render() {     return (       <div>         shouldComponentUpdate demo          <p><b>{this.state.value.toString()}</b></p>         <p>Count of clicks: <b>{this.state.countOfClicks}</b></p>         <button onClick={this.pickRandom}>           Click to randomly select: true or false         </button>       </div>     );   } }  ReactDOM.render(   <App />,   document.getElementById('app') ); 
like image 90
Piotr Berebecki Avatar answered Sep 28 '22 02:09

Piotr Berebecki