问题
Iam new to React and i came across with this doc. It says that:
Either way, it is unsafe to use componentWillUpdate for this purpose in async mode, because the external callback might get called multiple times for a single update
How is it possible for the componentWillUpdate to be called multiple times? It doesnt explain it.
Thank you
回答1:
On async mode, your component update/render may be postponed so react can deliver some hi-priority stuff. This means willUpdate
will be called each time react starts working on your component, but it might not finish the full update, hence calling willUpdate
every time it starts working on this component but only calls didUpdate
once, after this process is finished.
回答2:
Any change that is supposed to trigger a render will first go through a componentWillUpdate lifecycle. The change change can be a parent re-render causing the child to re-render, a change in components props, or a change in state.
However from v16.3.0 this lifecycle method is deprecated and it is encouraged that any sideeffect be handled in componentDidUpdate which will be triggered after render method.
来源:https://stackoverflow.com/questions/54533907/react-componentwillupdate-getting-called-twice