javascript - 是否需要在未安装的元素/组件上删除事件侦听器?
问题描述
如果我将事件侦听器附加到组件,是否需要在卸载时显式删除它?
例如
componentRef = React.createRef();
handleWheel = (e) => {
e.preventDefault();
}
componentDidMount() {
if (this.componentRef.current) {
this.componentRef.current.addEventListener('wheel', this.handleWheel);
}
}
componentWillUnmount() {
if (this.componentRef.current) {
this.componentRef.current.removeEventListener('wheel', this.handleWheel);
}
}
render() {
<Container ref={this.componentRef}>...</Container>
}
所以我想知道componentWillUnmount
在这种情况下是否需要
解决方案
推荐阅读
- reactjs - 如何使用带有外部依赖项的 React useCallback 和 useEffect
- unix - 复制并替换另一个目录中不同名称的文件
- excel - 从访问表中复制表并粘贴到空白的 Excel 文件中
- java - 如何用 clp-java 解决一个简单的线性问题
- javascript - 儿童道具是数组吗?
- javascript - 使用 jQuery 动态添加类在表单上触发 javascript 事件
- database - 如何处理 SCD 类型 2 维度和重复维度记录?
- windows - 如何将整个txt文件内容放在一个变量中
- html - 使用 css 设置 div 样式,使其看起来与默认的 html 选择表单一模一样
- python - 将动态函数应用于相空间中的每个点(由 2D 矩阵表示)