Concerning the newly proposed React Effect Hook;
What are the advantages and use cases of the Effect
hook (useEffect()
)?
Why would it be preferable & how does it differ over componentDidMount/componentDidUpdate/componentWillUnmount
(performance/readability)?
The documentation states that:
Mutations, subscriptions, timers, logging, and other side effects are not allowed inside the main body of a function component (referred to as React’s render phase).
but I think it was already common knowledge to have these behaviors in lifecycle methods like componentDidUpdate, etc. instead of the render method.
There's also the mention that:
The function passed to useEffect will run after the render is committed to the screen.
but isn't that what componentDidMount
& componentDidUpdate
do anyways?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…