Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在React中shoulldcomponentupdate和componentWillUpdate之间有什么区别?_Reactjs - Fatal编程技术网

Reactjs 在React中shoulldcomponentupdate和componentWillUpdate之间有什么区别?

Reactjs 在React中shoulldcomponentupdate和componentWillUpdate之间有什么区别?,reactjs,Reactjs,我有一个疑问,当我们使用react组件方法时,有一些方法具有相同的行为,并且它们在组件生命周期中被逐个调用?那么主要的区别是什么, shouldComponentUpdate(nextProps, nextState){ // It will be executed with nextProps and nextState } componentWillUpdate(nextProps, nextState){ // It will be executed first with sam

我有一个疑问,当我们使用react组件方法时,有一些方法具有相同的行为,并且它们在组件生命周期中被逐个调用?那么主要的区别是什么,

shouldComponentUpdate(nextProps, nextState){
  // It will be executed with nextProps and nextState
} 
componentWillUpdate(nextProps, nextState){
  // It will be executed first with same data nextProps, nextState
} 
在上面的示例中,我们有相同行为的相同参数数据,但是首先调用shouldComponentUpdate

那么主要区别是什么?

如果函数shouldComponentUpdate返回true,则将执行函数ComponentWillUpdate

如果shouldComponentUpdate返回false,则不会调用ComponentWillUpdate

以下是包含两个功能的文档:和

你可以在这里阅读


希望能对您有所帮助。

阅读文档。这里已经解释清楚了。您的代码段在两种情况下都使用了相同的方法。我投票将这个问题作为离题题来结束,因为OP没有花时间阅读官方文档>使用shouldComponentUpdate()来让React知道组件的输出是否不受当前状态或道具更改的影响。默认行为是在每次状态更改时重新渲染,在绝大多数情况下,您应该依赖默认行为。感谢所有的负票。。。需要深入研究为什么我们有这种方法,因为您可以从shouldComponentUpdate返回false,但不能从ComponentWillUpdate返回false。第二个似乎毫无意义。React也这么认为,因为他们不推荐使用ComponentWillUpdate。