Ива*_*нов 5 reactjs react-hooks
cat.destroy()当组件死掉时,我如何通过方法进行内存清理?
const object = useMemo(() => {
return new Cat()
}, [])
Run Code Online (Sandbox Code Playgroud)
清理组件卸载时执行的钩子的效果。它由钩子处的返回函数执行useEffect:
useEffect(() => {
// return a function to be executed at component unmount
return () => object.destroy()
}, [object])
Run Code Online (Sandbox Code Playgroud)
注意:正如 @Guillaume Racicot 指出的那样,在某些情况下,执行 unmount 时对象可能尚未创建,因此您将面临错误。在这种情况下,请记住有条件地执行destroy,您可以object?.destroy()为此使用可选链接。
| 归档时间: |
|
| 查看次数: |
7954 次 |
| 最近记录: |