是否有一个函数,例如componentWillUnmount,将在刷新页面之前触发 [英] Is there a function, like componentWillUnmount, that will fire before the page is refreshed
本文介绍了是否有一个函数,例如componentWillUnmount,将在刷新页面之前触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个函数,以便在用户刷新页面之前将请求发送到服务器(不关心响应).
I want a function, to send a request to a server (don't care about response) before a user refreshes the page.
我尝试使用 componentWillUnmount
方法,但是页面刷新未调用此函数.例如
I've tried using the componentWillUnmount
approach but the page refresh doesn't call this function. e.g.
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
}
}, []) }
有人知道如何做到这一点吗?
Does anyone have any ideas how to do this?
推荐答案
查看全文