2

In React, this.props.history.goback() and this.props.history.go(-1) are used to go back to the previous page in the browser. However, these cause the page to hard-reset (i.e., reload) when redirecting to the previous page.

Is there any way to prevent the page from refreshing in an SPA using React?

IronFlare
  • 2,287
  • 2
  • 17
  • 27
Rahul
  • 39
  • 1
  • 3
  • 1
    Possible duplicate of [Intercept/handle browser's back button in React-router?](https://stackoverflow.com/questions/39342195/intercept-handle-browsers-back-button-in-react-router) – Akin Okegbile Sep 09 '19 at 15:41

2 Answers2

1

If you use BrowserRouter, there should be no reloading within your origin.

import { BrowserRouter } from 'react-router-dom'

<BrowserRouter>
    <App/>
</BrowserRouter>
Nappy
  • 3,016
  • 27
  • 39
0

You could try using window.history.back().

HTML

<button onclick="goBack()">Go Back</button>

JavaScript

function goBack() {
    window.history.back();
}
Azametzin
  • 5,223
  • 12
  • 28
  • 46
Remy Baas
  • 1
  • 3