How to send API call before page reload or close using ReactJS ? Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To send an API call before the page reloads or closes using React JS we have to link or define a function when the page unloads. The page unloading happens just before the page is closed or tries to reload.PrerequisitesReact JSHTML DOM onbeforeinload eventApproachWe will use some listeners like "beforeupload" and "upload", which trigger a function that sends API calls. The listener will only trigger the function before the page reloads or closes so that we have the updated result from the API.Creating React ApplicationStep 1:Use this command in the terminal to start a react project.npx create-react-app <folder-name>Step 2: Move to the project directorycd <folder-name>Project StructureThe project structure will look like this.Example: Example demonstrates sending API call before page reload using beforeunload event listener. JavaScript import { useState, useEffect } from "react"; function App() { const [data, setData] = useState("Loading..."); const getData = () => { fetch("https://api.adviceslip.com/advice") .then((response) => response.json()) .then((data) => { setData(data.slip.advice); }); } useEffect(() => { getData(); }, []); window.addEventListener("beforeunload", (event) => { getData(); console.log("API call before page reload"); }); window.addEventListener("unload", (event) => { getData(); console.log("API call after page reload"); }); return ( <div> <h1>GeeksforGeeks</h1> <h3>{data}</h3> </div> ); } export default App; Steps to run the Application: Use this command in the terminal in project directory.npm startOutput: This output will be visible on http://localhost:3000/ on the browser window. Comment More infoAdvertise with us Next Article How to Create RESTful API and Fetch Data using ReactJS ? D devaadi Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to log-out user from app using ReactJS ? The objective of this article is to how we can set up, log out a user, and retrieve data from local memory User Browser storage in the React app. During the sign-in session, we will save the user details to the browser's local storage, and also during the time of logout, we will remove the user's de 3 min read How to prevent page from rendering before redirect in ReactJS ? ReactJS is a popular front-end framework that allows developers to create dynamic and responsive web applications. One common issue that developers face when working with ReactJS is preventing a page from rendering before a redirect is complete. This can lead to issues such as slow loading times, br 4 min read How to Create RESTful API and Fetch Data using ReactJS ? React JS is more than just an open-source JavaScript library, it's a powerful tool for crafting user interfaces with unparalleled efficiency and clarity. One of React's core principles is its component-based architecture, which aligns perfectly with the Model View Controller (MVC) pattern. React com 5 min read How to Use Redux Toolkit in React For Making API Calls? In this article, we will explore how to use the Redux Toolkit in a React.js application to streamline state management and handle asynchronous operations, such as making API calls. Redux Toolkit's createAsyncThunk simplifies the creation of asynchronous action creators, allowing us to efficiently fe 4 min read How to Redirect to Another Page in ReactJS ? In ReactJS, when you need to move users from one page to another, we can use the built-in React Router library, which is designed specifically for handling navigation within a React application.ApproachUse the Navigate component: In React Router v6, the Navigate component helps us to redirect users 6 min read How to save new state to local JSON using ReactJS ? To save data in local storage is a common task while creating a react application to store the user-specific data. We can save new state to local JSON using react in local storage of the browser and access that information anytime later. Prerequisites:React JSNPM & Node.jsJavaScript localStorage 2 min read Like