在用户离开我的网页或浏览器时,我需要发送一些数据。 哪些事件应当使用? 我正在使用反应堆和轴心,或许可以采取某种办法。
我试图使用卸载、卸载和上载,但定期工作。 有时它发送数据,但当我试图再次这样做时,它就没有工作。 而且,我不需要一个登峰/标签表格,该表要求用户确认填写表格,以显示该页即将关闭。
在用户离开我的网页或浏览器时,我需要发送一些数据。 哪些事件应当使用? 我正在使用反应堆和轴心,或许可以采取某种办法。
我试图使用卸载、卸载和上载,但定期工作。 有时它发送数据,但当我试图再次这样做时,它就没有工作。 而且,我不需要一个登峰/标签表格,该表要求用户确认填写表格,以显示该页即将关闭。
我认为,当用户离开你的网页或关闭浏览器时,你想要发送数据时,<代码>在<<>unload>之前即为一种常用的方法。
Using beforeunload
event can trigger a confirmation dialog in some browsers, and there s no guaranteed time for the network request to complete.
One alternative is to use the unload
event, which is fired just before the page is unloaded. Keep in mind that, similar to beforeunload
, the unload
event is also limited in terms of reliability for sending data.
添加使用<代码>unload的样本代码:
import React, { useEffect } from react ;
import axios from axios ;
const MyComponent = () => {
useEffect(() => {
const cleanup = async () => {
try {
await axios.post( your-api-endpoint , { yourData });
} catch (error) {
console.error( Error sending data on unload: , error);
}
};
window.addEventListener( unload , cleanup);
return () => {
cleanup();
window.removeEventListener( unload , cleanup);
};
}, []);
return (
<div>
{/* Your component content */}
</div>
);
};
export default MyComponent;
This is my first question, and english is not my first language, sorry if my question is hard to understand I was trying to make simple CRUD app with mysql database and react as the frontend, and i ...
I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...
这是我的第一个问题,请与我一起回答。 I m从事一项SPA React项目,该项目跟踪农场及其相关床位,并可使用一些帮助。 我愿就......提供一些数据。
I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...
I have this custom filter for date , filter button is working as expected but reset button is not working, can anyone help me,what s wrong here? Below is my code which contain handlesearch,handlereset ...
I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...
I am trying to make a POC with Rails5, action Cable, React and Rails and React DnD. The purpose is to make an app like trello but for an recruitment process. My front is in ReactJS. I have 3 ...