react如何卸载组件_React - 组件移除 | reactjs Tutorial
例
componentWillUnmount()
在从DOM卸载组件之前调用此方法。
这是一个执行清洁操作的好地方,例如:删除事件侦听器。
清除计时器。
停止套接字。
清理redux状态。componentWillUnmount(){
...
}
在componentWillUnMount中删除附加事件侦听器的示例import React, { Component } from 'react';
export default class SideMenu extends Component {
constructor(props) {
super(props);
this.state = {
...
};
this.openMenu = this.openMenu.bind(this);
this.closeMenu = this.closeMenu.bind(this);
}
componentDidMount() {
document.addEventListener("click", this.closeMenu);
}
componentWillUnmount() {
document.removeEventListener("click", this.closeMenu);
}
openMenu() {
...
}
closeMenu() {
...
}
render() {
return (
href = "javascript:void(0)"
className = "closebtn"
onClick = {this.closeMenu}
>
×
Some other structure
);
}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
