我正在尝试使用浏览器的后退按钮,以便用户可以在我的 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/