来自官方教程:

在卸载和销毁组件之前立即调用componentWillUnmount().在此方法中执行任何必要的清理,例如使计时器无效、取消网络请求或清理componentDidMount中创建的任何DOM元素

我理解"使计时器失效".fetch可以用AbortController中止.但我不明白"清理componentDidMount中创建的任何DOM元素",我能看看这种情况的例子吗?

推荐答案

如果网络请求发送库支持中止正在进行的网络请求调用,那么您完全可以在componentWillUnmount方法中调用它.

然而,关于DOM个元素的清理是令人担忧的.根据我目前的经验,我将举几个例子.

First one is -

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>
        );
    }
}

在这里,我删除了在安装组件时添加的click event listener.

Second one is -

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>
        );
    }
}

在这里,我试图将d3.js与react整合到componentWillUnmount中;我正在从DOM中删除图表元素.

除此之外,我还用了componentWillUnmount个来清理开机模式.

我确信还有很多其他的用例,但这些都是我使用过的componentWillUnMount个用例.我希望它能帮助你.

Reactjs相关问答推荐

使用useReducer时,props 未从父级传递给子或孙级

Tailwind不使用@apply classes构建,并强制使用类似于移动的viewport

有没有一种惯用的方式来接受特定的子元素?

基本react 应用程序正在触发两次Use Effect

在Map()完成React.js中的多个垃圾API请求后执行函数

React useEffect 问题...异步函数内的变量正在获取延迟的更新值

React 错误无法读取未定义的属性(读取id)#react

设置自定义形状的纹理

VideoSDK api 不使用更新状态

为什么刷新页面时我的localStorage PET值变成空字符串?

状态链接未传递到路由页面

如何在 nextjs 中单击按钮时动态导入和渲染组件?

使用 React.lazy 调试 webpack 代码拆分

当我在 useEffect 中使用 useDispatch 时,我的组件继续渲染

将 Material UI 菜单渲染为

如何定制 React 热 toastr ?

在 React 中,为什么不能向空片段添加键(短语法)?

如何在 React JS 上使文本中的单词可点击

交换键仍然引入了 fresh 的 DOM 元素

我在使用 Elements of stripe 时使用 React router v6