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 ...
}
网友评论