How to Refresh Pages in React With One Line of Code

Using Vanilla JavaScript
Ferenc Almasi β€’ Last updated 2023 November 11 β€’ Read time 4 min read
  • twitter
  • facebook
React

To refresh a page in React, you don't need React itself β€” just plain JavaScript. Call window.location.reload, which triggers a full page reload.

Copied to clipboard! Playground
export const App = () => {
    const refresh = () => window.location.reload(true)

    return (
        <button onClick={refresh}>Refresh</button>
    )
}
Refresh function in React

This functions like a refresh button. Note that we pass true to the reload method. This non-standard feature is supported only in Firefox, instructing the browser to bypass the cache and force a reload.

Using a boolean parameter for location.reload is ignored by all other browsers except Firefox.

Alternatively, we can achieve this using an inline onClick handler:

Copied to clipboard!
<button onClick={() => window.location.reload(true)}>Refresh</button>
Refresh page using a button

However, in most cases, consider updating your state with useState instead of a full page reload for a better user experience. Use the above solution only when a complete page reload is necessary.


Refresh by Updating the State

If you don't want a full page reload, refresh the page by updating the state. In the following example, we have a table where values are calculated based on other fields. Try changing the first and last name cells in the interactive widget below to update the state.

  • /Table.jsx Table.jsx
  • /App.jsx App.jsx
  • /data.js data.js
Refresh

Using the useState hook, we can refresh the table and the page without a page reload. The updateTable function is called on input changes, updating both the state and UI.

For demonstration purposes, the above code has been shortened. In a real-world scenario, make sure to use thead and tbody for correct semantics.


Refresh by Resetting State

Another approach to page refresh without a full reload is to reset the state back to its original values. This can be done with a reset button that calls the updater function of the useState hook with the initial values, essentially, resetting the page. Update the code in the interactive widget above according to the following to reset the state:

Copied to clipboard! Playground
return (
    <React.Fragment>
        ...
        </table>
        <button onClick={() => setUsers(data)}>Reload</button>
    </React.Fragment>
)
Table.jsx
Reset state in React

Don't forget to wrap the table and button elements in a fragment and pass the initial object to setUsers to reset the state.

Looking to improve your skills? Check out our interactive course to master React from start to finish.
Master Reactinfo Remove ads

Maintaining State After Refresh

There can also be cases where we want to do a full page reload, but keep track of the state after the reload. This can be achieved through the use of the Local Storage API. Local storage helps us keep data in the user's browser. For this, we'll need to introduce the following two functionalities:

  1. Capture the updated state and store it in local storage.
  2. Create a useEffect hook to reset the state to its previous value on page load.
  • /Table.jsx Table.jsx
  • /App.jsx App.jsx
  • /data.js data.js
Refresh

Try changing the values in the table in the above code example, then click on the "Refresh Page" button. The state will be saved through page reloads. The state is saved on line:21 and reset on line:29.

Note that we need to use JSON.stringify and JSON.parse to save and retrieve the data from Local Storage. The API handles only strings, so we need to handle conversions.

Once the state is set, we can use JSON.parse to turn the string back into a JavaScript object and pass it to setUsers to update the state.


Refresh Every X Seconds

Last but not least, it's also possible to refresh the page every X seconds, if you need to achieve automatic refreshes. This can be done by wrapping the very first solution into a setTimeout in the following way:

Copied to clipboard!
setTimeout(() => { 
    window.location.reload()
}, 10_000)
Refresh page every ten seconds

This will refresh the page every 10 seconds. When the page is reloaded, the setTimeout will be called automatically, which executes the wrapped code after a set of milliseconds, passed as the second parameter. In our case, this is set to 10 seconds or 10,000 milliseconds.

Note that you can use underscore in numbers for JavaScript to make them more readable. The above example is equivalent to 10000.


Summary

In short, for a full-page reload in React, use window.location.reload. Otherwise, consider the following:

  • Refresh page without a reload: Use useState to update the internal state.
  • Refresh page to initial value: Call the useState updater function with initial values.
  • Maintain state after reload: Use local storage to store and restore the state.
  • Refresh page every X seconds: Wrap your code in a setTimeout.
  • twitter
  • facebook
React
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.