Detect Page Refresh, Tab Close and Route Change with React Router v5

By DEV Community - 2021-03-10

Description

Imagine accidentally closing the browser tab after filling a mandatory and boring survey form. All yo... Tagged with javascript, react, webdev, reactrouter.

Summary

  • Imagine accidentally closing the browser tab after filling a mandatory and boring survey form.
  • functionMyComponent(){const[showExitPrompt,setShowExitPrompt]=useState(false);// Initialize the beforeunload event listener after the resources are loadedwindow.onload=function(){initBeforeUnLoad(showExitPrompt);};} # Enter fullscreen modeExit fullscreen mode OR #2 Component file App.js Pass it down to your child components via Context.
  • history.pushState() only changes the URL nothing else, the whole page stays intact while location.pathname redirects you to that new page, probably giving a 404 error because such a route does not exist.

 

Topics

  1. Frontend (0.51)
  2. UX (0.34)
  3. Mobile (0.05)

Similar Articles

sudheerj/reactjs-interview-questions

By GitHub - 2021-01-12

List of top 500 ReactJS Interview Questions & Answers....Coding exercise questions are coming soon!! - sudheerj/reactjs-interview-questions

Server-Side Rendering (SSR) with React

By DEV Community - 2021-02-28

by Author Samaila Bala Server Side rendering has been the popular way of rendering web applications... Tagged with react, webdev, javascript.

8 Awesome React Hooks

By DEV Community - 2020-12-30

React.js is currently the most popular JavaScript library for front end developers. Invented by Faceb... Tagged with react, javascript, webdev, beginners.

Handling multi-page API calls with React Hooks

By DEV Community - 2021-01-01

Today we will be walking through an example of how to make continuous API calls to fetch multiple pag... Tagged with react, webdev, frontend, javascript.