Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between shouldComponentUpdate and componentWillUpdate in React?

Tags:

reactjs

I have a doubt , when we are using react component methods there are methods with same behavior, and also they are called one by one in the component life cycle ? then what is the major difference,

shouldComponentUpdate(nextProps, nextState){
  // It will be executed with nextProps and nextState
} 
componentWillUpdate(nextProps, nextState){
  // It will be executed first with same data nextProps, nextState
} 

In the above example We have same parameter data with same behavior but the shouldComponentUpdate will be called first.

Then what is the main difference ?

like image 906
Gopinath Kaliappan Avatar asked Dec 18 '22 05:12

Gopinath Kaliappan


1 Answers

The function ComponentWillUpdate will be executed, if function shouldComponentUpdate returns true.

If shouldComponentUpdate returns false, then ComponentWillUpdate will not be called.

Here is document of 2 functions : componentwillupdate and shouldcomponentupdate.

You can read here component-lifecycle

I hope it's helpful to you.

like image 168
Voi Mập Avatar answered Dec 31 '22 01:12

Voi Mập