Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does nextState do in shouldComponentUpdate?

In the React lifecycle function shouldComponentUpdate(nextProps, nextState), nextProps is self explanatory.

But what does nextState do?

It doesn't sound right that I can evaluate upcoming state before even deciding if the component should be rendered/modified or not.

like image 712
Fellow Stranger Avatar asked Feb 05 '18 00:02

Fellow Stranger


People also ask

What happens if shouldComponentUpdate returns false?

ReactJS shouldComponentUpdate() Method Return value: It by default it returns true and if it returns false then render(), componentWillUpdate() and componentDidUpdate() method does not gets invoked.

Which overrules shouldComponentUpdate () in React?

PureComponent overrides shouldComponentUpdate() lifecycle method. React. PureComponent implements shouldComponentUpdate() method to determine re-rendering with a shallow prop and state comparison for rendering performance optimization.

When should you use shouldComponentUpdate?

shouldComponentUpdate use to decide that changes in props or the state have affected to component output or not. componentDidUpdate will be triggered when there is an update on component output. shoudComponentUpdate take 2 arguments, nextProps and nextState . that contains a new update of props and state.

Can we use setState in shouldComponentUpdate?

shouldComponentUpdate() Safe to use setState ? Yes! This method is called whenever there is an update in the props or state. This method has arguments called nextProps and nextState can be compared with current props and currentState .


1 Answers

Basically the state is already changed at that point and do you deem it necessary to rerender the component and based on that you return true or false

like image 138
mu_sa Avatar answered Nov 15 '22 11:11

mu_sa