DIX*_*mar 4 reactjs react-hooks react-lifecycle-hooks
如何通过反应钩子在功能组件中使用 componentWillUpdate ?
小智 8
您可以 使用 refs 和效果挂钩的组合创建与componentWillUpdate相同的效果。
文档说 componentWillUpdate,每次更新时都会调用,发生。它不会在初始渲染时调用。
现在使用useRef钩子,返回对象在应用程序的完整生命周期中持久化。
const isFirstRender = React.useRef(true);
React.useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
/*business logic for component did update*/
});Run Code Online (Sandbox Code Playgroud)
小智 -1
// similar to componentWillUpdate
useMemo(()=>{
doSomething()
}, [dep1, dep2])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6959 次 |
| 最近记录: |