简体   繁体   English

防止历史导航导致在React中重新加载

[英]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. 在React中, this.props.history.goback()this.props.history.go(-1)用于返回浏览器中的上一页。 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? 有什么方法可以阻止页面使用React在SPA中刷新吗?

If you use BrowserRouter , there should be no reloading within your origin. 如果您使用BrowserRouter ,则您的原点内不应进行任何重载。

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

<BrowserRouter>
    <App/>
</BrowserRouter>

You could try using window.history.back() . 您可以尝试使用window.history.back()

HTML 的HTML

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

JavaScript 的JavaScript

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 取消导航时如何防止后退和前进浏览器按钮导致页面重新加载并丢弃state。 React-router v4.3 - How do you prevent the back and forward broswer buttons from causing the page to reload and drop state when canceling navigation. React-router v4.3 React 导航重置历史记录 - React Navigation Reset History 限制导航以防止浏览器挂起 - React - Throttling navigation to prevent the browser from hanging - React react-router:如何在不导致导航/重新加载的情况下更新 url? - react-router: How do I update the url without causing a navigation/reload? history.pushState()导致浏览器导航 - history.pushState() causing browser navigation 如何防止Enter导致Dojo网站重新加载 - How to prevent Enter causing Dojo website to reload 如何防止form.target导致历史记录中的所有HTTP GET再次往返服务器? - How to prevent form.target from causing all HTTP GET in history from doing another roundtrip to the server? 如何防止图像在 react native 中的 uri 重新加载时闪烁? - how to prevent image from flickering on uri reload in react native? 防止KeyboardAvoidingView导致内容重叠(React Native)? - Prevent KeyboardAvoidingView from causing content to overlap (React Native)? 在 Angular 8 中使用 window.history.state 导致页面重新加载错误 - Using window.history.state Causing Error On Page Reload In Angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM