You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've come across the case several times where I have had a flow with several entries, a number of intermediate screens that vary depending on the entry and a single end screen. As in the following diagram.
The problem is that at the end of the flow, it's impossible to return to the beginning (Entry A, B or C) using pop (because the number of intermediate screens can vary) or navigate (because we have several entries).
Nested navigation could be a solution, but it increases complexity and introduce bugs. In addition, nested navigation is usually not recommend.
So the goal is to find a simple solution to go back to the beginning of a flow that could be implemented in react-navigation.
First alternative
A first alternative is to store a navigate function in the entry screen and to call it in the last screen.
We don’t need to have any information about intermediate screens, but I am not sure that can be implemented in react navigation and this requires to call the store in two different screens.
constuseGoBackHere=()=>{const{ navigate }=useNavigation()constroute=useRoute<RouteProp<ReactNavigation.RootParamList>>()return()=>navigate(route.name,{ ...route.params}asany)//Typing to fix}
Second alternative
A second alternative goBackWithFilter would allow us to specify an array of intermediate screens and remove all these screens from the navigation as well as the current screen in order to return to the beginning of the flow. So we can go back to the start of any flow, regardless of the entry point.
Here is the code of this solution
import{CommonActions,useNavigation}from"@react-navigation/native";import*asRfrom"remeda";importtype{RootStackParamList}from"#app/navigation/navigation.types";exportconstuseGoBackWithFilter=()=>{constnavigation=useNavigation();constgoBackWithFilter=(screensToIgnore: (keyofRootStackParamList)[]=[])=>{navigation.dispatch((state)=>{constroutes=[...state.routes];constcurrentRoute=routes[routes.length-1]?.name;constnewRoutes=R.pipe(routes,R.filter((screen)=>![...screensToIgnore,currentRoute].includes(screen.name)),);returnCommonActions.reset({
...state,routes: newRoutes,index: newRoutes.length-1,// Setting right index is important to handle goBack animation});});};return{ goBackWithFilter };};
This implementation is probably not the best and not enough generic but I think this idea could be implemented in the library
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Why ?
I've come across the case several times where I have had a flow with several entries, a number of intermediate screens that vary depending on the entry and a single end screen. As in the following diagram.
The problem is that at the end of the flow, it's impossible to return to the beginning (Entry A, B or C) using
pop(because the number of intermediate screens can vary) ornavigate(because we have several entries).Nested navigation could be a solution, but it increases complexity and introduce bugs. In addition, nested navigation is usually not recommend.
So the goal is to find a simple solution to go back to the beginning of a flow that could be implemented in react-navigation.
First alternative
A first alternative is to store a navigate function in the entry screen and to call it in the last screen.
We don’t need to have any information about intermediate screens, but I am not sure that can be implemented in react navigation and this requires to call the store in two different screens.
Second alternative
A second alternative
goBackWithFilterwould allow us to specify an array of intermediate screens and remove all these screens from the navigation as well as the current screen in order to return to the beginning of the flow. So we can go back to the start of any flow, regardless of the entry point.Here is the code of this solution
This implementation is probably not the best and not enough generic but I think this idea could be implemented in the library
Beta Was this translation helpful? Give feedback.
All reactions