简体   繁体   中英

Prevent history navigation from causing reload in React

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 (ie, reload) when redirecting to the previous page.

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

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

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

<BrowserRouter>
    <App/>
</BrowserRouter>

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

HTML

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

JavaScript

function goBack() {
    window.history.back();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM