问题
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