ReactJS: What is the difference between componentDidUpdate and the second parameter of setState?
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.
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.