React-native-router-flux: Action.popTo not throwing try/catch when there is no existing scene

Created on 16 Nov 2018  路  4Comments  路  Source: aksonov/react-native-router-flux

Version

Tell us which versions you are using:

  • react-native-router-flux v4.0.6
  • react v16.6.0-alpha.8af6728
  • react-native v0.57.4

Expected behaviour

To persist dynamic scenes on navigation, I created a try/catch block with an Action.popTo method that will call Action.key whenever there are no scenes to popTo (using try/catch). I have used this up to RN 0.40/RNRF 0.38 and it was working as expected.

Actual behaviour

However, upon upgrading the project to the latest versions (RN 0.57/RNRF 4.0.6) the popTo method would now show a "cannot find routeName" type of warning and would no longer proceed to the catch block. With this, I cannot find an alternative anywhere to what I want to accomplish (popTo a scene if it exists, else push a new one)

Issues that come with minimal repro's are resolved much more quickly than issues where a maintainer has to reproduce themselves.

Most helpful comment

A Simple work around would be

  1. Access the navigation state using Actions.state
  2. get the route stack using Actions.state.routes
  3. find the routeName you want to popTo using the JS find function
  4. If your route exists then popTo that route else popTo an alternative
let routeStack = Actions.state.routes
let routeNameToPopTo = routeStack.find(({routeName})=>routeName===yourRouteKeyToPopTo)
if(routeNameToPopTo)
    Actions.popTo(routeNameToPopTo)
else // your alternative route to popTo

Hope this helps, I know it is a little cumbersome, but it provides more flexibility.

All 4 comments

Yes, if Action methods could return a Promise we could popTo a fallback route incase the route we are trying to popTo is not available.

A Simple work around would be

  1. Access the navigation state using Actions.state
  2. get the route stack using Actions.state.routes
  3. find the routeName you want to popTo using the JS find function
  4. If your route exists then popTo that route else popTo an alternative
let routeStack = Actions.state.routes
let routeNameToPopTo = routeStack.find(({routeName})=>routeName===yourRouteKeyToPopTo)
if(routeNameToPopTo)
    Actions.popTo(routeNameToPopTo)
else // your alternative route to popTo

Hope this helps, I know it is a little cumbersome, but it provides more flexibility.

A Simple work around would be

  1. Access the navigation state using Actions.state
  2. get the route stack using Actions.state.routes
  3. find the routeName you want to popTo using the JS find function
  4. If your route exists then popTo that route else popTo an alternative
let routeStack = Actions.state.routes
let routeNameToPopTo = routeStack.find(({routeName})=>routeName===yourRouteKeyToPopTo)
if(routeNameToPopTo)
    Actions.popTo(routeNameToPopTo)
else // your alternative route to popTo

Hope this helps, I know it is a little cumbersome, but it provides more flexibility.

It's a great help.
Thanks for this! Will try it out.

@shubhang93 thanks, closing this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maphongba008 picture maphongba008  路  3Comments

VictorK1902 picture VictorK1902  路  3Comments

sylvainbaronnet picture sylvainbaronnet  路  3Comments

GCour picture GCour  路  3Comments

booboothefool picture booboothefool  路  3Comments