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'}); }
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.
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.
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.
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.
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.
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.
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.
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 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') );
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