reactjs - 后退按钮在 React 中显示空白页面

标签 reactjs react-router react-router-dom

我正在尝试使用浏览器的后退按钮,以便用户可以在我的 React 应用程序中顺利返回。它有时适用于特定组件。例如,当我从 Supervisors 转到 Anfitrion 时它有效,但当我从 Anfitrion 转到 Suprvisores 或任何动态路线时它不起作用。 这是我的路由器中的内容。

    <Router>

    <Switch>

            <Route exact path="/Anfitrion" component={PropiedadesRegistradas}/>
            <Route path='/AgregarPropiedad' component={AgregarPropiedad} />
            <Route path= "/AreasRegistradas/:propertyId" component={AreasRegistradas}/>
            <Route path= "/ItemsAreasRegistradas/:propertyId" component={ItemsAreasRegistradas}/>            
            <Route path="/ElementosDeArea/:areaId" component = {ElementosDeArea} />
            <Route path="/AgregarItems/:areaId/:propertyId" component = {AgregarItems} />
            <Route path="/AgregarArea/:propertyId" component = {AgregarArea} />

            <Route path="/supervisores" component={Supervisores}/>
            <Route path="/historial" component={Historial}/>
            <Route path="/desinfeccion" component={Desinfeccion}/>
            <Route path="/folios" component={Folios}/>    

            <Route path="/areasRegistradas" component={AreasRegistradas}/>
            <Route path= "/agregarSupervisor" component={AgregarSupervisor}/>
            <Route path="/detalle/:servicio/:hostId" component={Detalle} />
            <Route render={()=> <h3>Not found</h3>}/>

          </Switch>
    </Router>


下面是我尝试使用 useHistory Hook 的结果,但是当我点击按钮时它显示黑页:

import React from 'react'
import Button from '@material-ui/core/Button';
import { useHistory } from 'react-router-dom'



export default function AgregarItems({match}, props){
    let history = useHistory()
    return(
        <div>
        <Button onClick={() => history.goBack()}>Atrás</Button>
        </div>
    )
}

当我使用浏览器的后退和前进按钮时,它也会显示一个空白页面。我正在阅读有关 React 路由器的历史,但我还没有发现任何有用的东西,当我这样做是为了 React Router v3。

感谢您的帮助!

我正在使用:

  • "react-dom": "^16.13.1",
  • " react 路由器": "^5.2.0",
  • "react-router-dom": "^5.2.0",

最佳答案

我通过从我的 Rails 应用程序中删除 turbolinks 解决了这个问题。信用:page is not rendering when I click back button with react-router

关于reactjs - 后退按钮在 React 中显示空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62942573/

相关文章:

javascript - react 。如何传递带有参数作为 prop 的函数?

reactjs - React-Quill - addRange() : The given range isn't in document

javascript - 如何通过react-router正确使用嵌套路由?

javascript - react 路由器 6.4.3 typescript - useLocation 任何类型错误

javascript - 在 React 中,如何返回 JSX 以及返回更多 JSX 的 JS 函数?

html - 使用 ReactToPrint 时,Chrome 中的横向设置不可用

javascript - React-router v3 中具有嵌套路由的索引路由

javascript - React Route - 从 JSON 对象中的字符串渲染路由中的组件

javascript - 使用react-router-dom的带有状态的重定向

javascript - react 原生 "Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)"