【发布时间】:2020-11-23 10:25:34
【问题描述】:
我想要一个函数,在用户刷新页面之前向服务器发送请求(不关心响应)。
我尝试过使用componentWillUnmount 方法,但页面刷新没有调用此函数。例如
import React, { useEffect } from 'react';
const ComponentExample => () => {
useEffect(() => {
return () => {
// componentWillUnmount in functional component.
// Anything in here is fired on component unmount.
// Call my server to do some work
}
}, []) }
有人知道怎么做吗?
【问题讨论】:
-
componentDidMount() 是您应该使用的函数。
-
检查 this 本地事件的答案
-
componentDidMount()将在组件加载时触发。我希望在页面即将刷新时触发一个函数。 -
感谢@kunquan,但该本地事件提供了一个确认对话框。我想要一个函数,所以我可以在页面刷新之前做一些工作
-
您可以在该函数中做一些工作。对话框只是默认行为。
标签: javascript reactjs