美文网首页
判断react销毁dom的几种方法

判断react销毁dom的几种方法

作者: ThemisHoo | 来源:发表于2019-12-10 15:47 被阅读0次
 componentWillUnmount() {
        // 组件卸载之后,打断所有未结束得请求,
        // 子类要是要使用componentWillUnmount,需要显示的调用super.componentWillUnmount();
    }
import { Prompt } from 'react-router-dom';

// 放render中
<Prompt message="确定要离开?"  when={true}/>

import React, { useEffect } from 'react';

const Editor=()=>{

    //监听窗口事件
       useEffect(() => {
           const listener = ev => {
               ev.preventDefault();
               ev.returnValue='文章要保存吼,确定离开吗?';
           };
           window.addEventListener('beforeunload', listener);
           return () => {
               window.removeEventListener('beforeunload', listener)
           }
       }, []);
       
   //return ...
   }

相关文章

网友评论

      本文标题:判断react销毁dom的几种方法

      本文链接:https://www.haomeiwen.com/subject/zjdggctx.html