Ali*_*ali 37 javascript reactjs
从官方教程:
componentWillUnmount()在卸载和销毁组件之前立即调用.在此方法中执行任何必要的清理,例如使计时器无效,取消网络请求或清除在中创建的任何DOM元素componentDidMount
我理解"使计时器无效".fetch可以中止AbortController.但我不明白"清理任何创建的DOM元素componentDidMount",我能看到这种情况的例子吗?
Wit*_*ult 34
如果网络请求发送库支持中止正在进行的网络请求调用,您肯定可以在componentWillUnmount方法中调用它.
然而,关于DOM元件的清理是关注的.根据我目前的经验,我将举几个例子.
第一个是 -
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 (
<div>
<a
href = "javascript:void(0)"
className = "closebtn"
onClick = {this.closeMenu}
>
×
</a>
<div>
Some other structure
</div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
这里我将删除我在组件安装时添加的click事件侦听器.
第二个是 -
import React from 'react';
import { Component } from 'react';
import ReactDom from 'react-dom';
import d3Chart from './d3charts';
export default class Chart extends Component {
static propTypes = {
data: React.PropTypes.array,
domain: React.PropTypes.object
};
constructor(props){
super(props);
}
componentDidMount(){
let el = ReactDom.findDOMNode(this);
d3Chart.create(el, {
width: '100%',
height: '300px'
}, this.getChartState());
}
componentDidUpdate() {
let el = ReactDom.findDOMNode(this);
d3Chart.update(el, this.getChartState());
}
getChartState() {
return {
data: this.props.data,
domain: this.props.domain
}
}
componentWillUnmount() {
let el = ReactDom.findDOMNode(this);
d3Chart.destroy(el);
}
render() {
return (
<div className="Chart">
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我试图d3.js与反应整合componentWillUnmount; 我正在从DOM中删除图表元素.
除此之外我componentWillUnmount打开后用于清理自举模态.
我相信还有很多其他用例,但这些是我使用过的情况componentWillUnMount.我希望它对你有所帮助.
在这个简单的例子中(例子取自 React Docs),我使用它来清除时钟组件的间隔。例如,在您的页面中,您有 2 个选项卡,其中一个正在显示User Info,第二个选项卡在User Schedule那里显示实时时钟。切换到User Schedule选项卡后,componentDidMount将调用以设置计时器。一旦你切换回User Info,就不需要保留这个间隔钩子,你可以在componentWillUnmount事件中编写你的取消绑定/取消订阅逻辑。
import React from "react";
export default class Clock extends React.Component {
constructor(props) {
console.log("Clock", "constructor");
super(props);
this.state = {
date: new Date()
};
}
tick() {
this.setState({
date: new Date()
});
}
// These methods are called "lifecycle hooks".
componentDidMount() {
console.log("Clock", "componentDidMount");
this.timerID = setInterval(() => {
this.tick();
}, 1000);
}
// These methods are called "lifecycle hooks".
componentWillUnmount() {
console.log("Clock", "componentWillUnmount");
clearInterval(this.timerID);
}
render() {
return (
<div>It is {this.state.date.toLocaleTimeString()}.</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Components使用React进行创建时,并非每个库都与希望管理DOM的哲学很好地集成在一起。
一个例子就是使用像c3这样的图形库。c3期望被赋予一个DOM节点,并将在React之外创建/管理它自己的标记。在这种情况下,当您从DOM中删除组件时,您应该管理清理此库创建的所有元素。
import React, { Component, PropTypes } from 'react';
import c3 from 'c3';
export default class Graph extends Component {
componentDidMount () {
this._initGraph();
}
componentWillUnmount () {
this.graph = this.graph.destroy();
}
_initGraph () {
this.graph = c3.generate({
bindto: this.refs.graph
});
}
render () {
return (
<div className="graph">
<div className="graph__graph" ref="graph"></div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,React创建一个单一div的占位符c3以添加其内容。此过程在componentDidMount生命周期挂钩中开始,并在中再次进行清理componentWillUnmount。
| 归档时间: |
|
| 查看次数: |
63090 次 |
| 最近记录: |