4

I need to get a previous scene key in componentWillMount using react-native-router-flux to checkout what screen I come back from and make some action on condition. I looked through a lot but haven`t found an answer. Are there any ways to do that ?

Radha Kotecha
  • 43
  • 1
  • 5

3 Answers3

9

You can achieve this via Actions.prevScene, which will return the key of the previous scene in your stack.

sftgf
  • 186
  • 1
  • 5
3

Its hard to tell, but easier is pass variable when navigating out, like there:

Actions.home({from: 'about'})

now, Home props contain from variable, and you can handle this.

dvvtms
  • 627
  • 3
  • 10
1

@sftgf's solution should work, but on react-native-router-flux 4.0.0-beta.28, it erroneously returns the current scene for me.

This Gist worked and has the added benefit of getting other scenes on the stack:

import {Actions} from react-native-reouter-flux

export function getPreviousScene (jumpCount = 1) { // if nothing is passed, it defaults to 1
  const index = Actions.prevState.routes.length - (jumpCount + 1) // because zero-based index

  if (index >= 0) {
    return Actions.prevState.routes[index].routeName
  }

  return 'home'
}