My app consists of a Stack that looks like this:
- Home
- Start
- Task details
- End task
When the user presses a TouchableOpacity in the End task screen I want to reset the stack and return to the Home screen. I’ve tried using
navigation.dispatch(StackActions.popToTop());
But this isn’t working. navigation
is a variable assigned to useNavigation()
. Is there a way to return to home using this hook?
3
Answers
If
Home
is the initial route of theStack
, then we can reset the stack to the initial route using thepopToTop
function of thenavigation
object.Furthermore, we do not need to use the
useNavigation
hook sinceEndTask
is a screen located in the stack navigator. It will be passed to the component by the framework.Here is a code snippet.
You can try,
try this