If user has navigated to another route, he can be navigated back to the previous page. If user has navigated back, he can be navigated forward to the previous page.
In react-native
, react-nativation
provides useNavigation hook so I can simply perform:
import React from 'react'
import { useNavigation} from 'react-navigation'
function Test()
{
const navigation = useNavigation()
return (
<div>
{ navigation.canGoBack() && <button onClick=(() => navigation.goBack())>Go Back</button>
</div>
)
}
in ReactJS
, react-router
provides useHistory
hook so I can perform history.goBack()
but not history.canGoBack()
.
import React from 'react'
import { useHistory } from 'react-router'
function Test()
{
const history = useHistory()
return (
<div>
{ history.canGoBack() && <button onClick=(() => history.goBack())>Go Back</button>
{/* history.canGoBack() is not a function */}
</div>
)
}
Does react-router provide a method to let us know if we can go back or forward?