点击上方蓝字关注我们
useEffect 不是简单的 didMount, didUpdate, willUnmount 的替代。但也一直无法准确地描述出来。本周读了文章 A complete Guide to useEffect 后,豁然开朗。在使用 Hooks 进行编程时,我们的 MindSet 需要彻底改变。 不再从生命周期建立我们的 Mental Model。
首先,组件的每一次渲染,props 和 state 都被 captured 而不再改变
就像电影中的每一帧一样。这得益于 React 的 Immutable 的设计。一旦我们建立了这种 MindSet,使得数据推理 UI 更加容易。
I like to think of React elements as being like frames in a movie. They capture what the UI should look like at a specific point in time. They don’t change. --- React as a UI Runtime
I think it’s ironic that Hooks rely so much on JavaScript closures, and yet it’s the class implementation that suffers from the canonical wrong-value-in-a