I'm trying to use the back button from the browser so the user can go back smoothly in my react application. It works sometimes for specific components. For example it works when I go from Supervisores to Anfitrion, but It doesn't work when I go from Anfitrion to Suprvisores nor in any of the dynamic Routes. Here's what I have in my Router.
<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>
And here's what I tried using the useHistory Hook, but it displays a black page when I click on the button:
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>
)
}
It also displays a blank page when I use the browser's back and fwd buttons. I was reading about history in React router but I haven't found anything useful yet and when I do it is for react Router v3.
Thanks for the help!
Im using:
- "react-dom": "^16.13.1",
- "react-router": "^5.2.0",
- "react-router-dom": "^5.2.0",