'navigation' is deprecated, instead, add this to the constructor:
const navigationEntries = window.performance.getEntriesByType('navigation');
if (navigationEntries.length > 0 && navigationEntries[0].type === 'reload') {
console.log("Page was reloaded");
}
Make sure to wrap it in the useEffect(). The reason for using useEffect in this case is to ensure that the code runs at the appropriate time during the component lifecycle.
When you place the code directly inside the function body without useEffect, it will be executed whenever the component function is called, which could lead to unexpected behavior.
By using useEffect with an empty dependency array ([]), you ensure that the code runs only once, after the initial render of the component. This approach is generally preferred when you want to perform initialization tasks or set up event listeners in a React component.
function MyComponent(){
useEffect(() => {
const navigationEntries = window.performance.getEntriesByType('navigation');
if (navigationEntries.length > 0 && navigationEntries[0].type === 'reload') {
console.log("Page was reloaded");
}
}, []);
}
export default MyComponent;
e.keyCode === 116
, which is the F5 key? Here is an example of how to do that: https://mcmap.net/q/93241/-how-to-handle-the-onkeypress-event-in-reactjs – Mcneese