ReactJS: What is the difference between componentDidUpdate and the second parameter of setState?

南楼画角 提交于 2019-12-12 08:45:21

问题


Is there any difference between those 2 functions? (besides their arguments) It seems like those methods have the same triggers, isn't it?

Appreciate your help :)


回答1:


componentDidUpdate is called whenever the component has re-rendered, which might be caused by either:

  • a change in state
  • a call to forceUpdate
  • a parent component re-rendering (or another call to React.render in the case of the top-level component)

The setState callback will only be called once the state transition has completed and the component has re-rendered.




回答2:


The difference is the usage of those two: componentDidUpdate() needs to be declared only once per component, while the callback argument of setState() needs to be passed in every place the setState() method is called.

It's like putting all your eggs in one basket: you don't need to carry around different bags, however if you might want to boil some of the eggs and some to make an omelette from, and in this case you use multiple bags.



来源:https://stackoverflow.com/questions/29799687/reactjs-what-is-the-difference-between-componentdidupdate-and-the-second-parame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!