我有一个 React 组件,它在安装时采用 dom 引用(我知道这是一种边缘情况)。我想知道是否有必要将托管 dom 的属性设置为 null
。或者 React 会处理它吗?
componentDidMount() {
this.elm = document.getElementById('foo')
}
componentWillUnmount(){
this.elm = null
}
最佳答案
通过 React 文档,您只需清理全局元素,例如使计时器无效、取消网络请求或清理在 componentDidMount() 中创建的任何订阅。
引用将在卸载周期中随组件一起销毁。
https://reactjs.org/docs/react-component.html#componentwillunmount
关于javascript - 当组件在 react 中卸载时,我应该删除对 DOM 元素的引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47884059/