判断react销毁dom的几种方法

2019-12-10  本文已影响0人  ThemisHoo
 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 ...
   }

上一篇 下一篇

猜你喜欢

热点阅读