react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Set `initialRouteName` and `initialRouteParams` based on props

Open dasmikko opened this issue 9 years ago • 73 comments

Hi

Is it possible to have a conditional homescreen?

Like, the app knows from the start that your not logged in, then automatically show the login screen, else show the "real" home.

Hope my question makes sense.

dasmikko avatar Feb 23 '17 15:02 dasmikko

Hey,

Assuming your static knowledge is:

const isLoggedIn = true;

you can just use initialRouteName property and pass it to StackNavigation to select the route.

Otherwise, you can:

  1. switch screen in render()
  2. switch navigator in render()

grabbou avatar Feb 23 '17 15:02 grabbou

You can't set initialRouteName and initialRouteParams based on props right now, but I think we need this.

satya164 avatar Feb 23 '17 15:02 satya164

Yeah, sounds like a valid use case. I'll take a look into that in the morning (I need it anyways for that exact purpose)

grabbou avatar Feb 23 '17 15:02 grabbou

@grabbou you can just use initialRouteName property and pass it to StackNavigation to select the route.

I'm not sure how to do this. I'm pretty new to ReactJS and React Native.

My StackNavigator is a constant, so I can't change any variables in the config for it.

dasmikko avatar Feb 23 '17 16:02 dasmikko

No worries. I just meant that if you now the variable at compile time (before render), at the time when you call StackNavigator(Routes), you can pass the 2nd argument { initialRouteName: isLoggedIn ? 'a' : 'b' }.

But as Satya said, you are probably talking about deciding inside render(), which we do not support yet. I'll land that tomorrow if no-one's faster :)

On Thu, 23 Feb 2017 at 17:02 dasmikko [email protected] wrote:

@grabbou https://github.com/grabbou you can just use initialRouteName property and pass it to StackNavigation to select the route.

I'm not sure what you mean by this. I'm pretty new to ReactJS and React Native.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/react-community/react-navigation/issues/458#issuecomment-282034350, or mute the thread https://github.com/notifications/unsubscribe-auth/ACWcxnIHfbc7DUbUG8uF0KeWiHIzEVXsks5rfa2dgaJpZM4MKHU2 .

grabbou avatar Feb 23 '17 16:02 grabbou

Now I understand.

Looking forward for the new feature! :)

dasmikko avatar Feb 23 '17 16:02 dasmikko

@jeffreymendez1993 That would add to the stack and cause the back button on android for example to take you to the "InitialScene" view. The gestures are still there.

Using the reset action should be all that is needed.

https://reactnavigation.org/docs/navigators/navigation-prop

BenMcH avatar Feb 27 '17 15:02 BenMcH

I'm a little consfused for when I should fire the reset.

Because I have a login screen, but I don't want to user to be able to go back.

dasmikko avatar Feb 27 '17 19:02 dasmikko

@dasmikko I made a method in my splash screen that takes the name of the route as an argument and I can just call that with the route name.

I can provide an example in a couple hours when I get home.

BenMcH avatar Feb 27 '17 21:02 BenMcH

Looking forward to see the example.

dasmikko avatar Feb 28 '17 06:02 dasmikko

Alternatively, you can have two navigators and if statement in render. Both use-cases are valid. I think for time-being this is a satisfactory solution and the issue can be closed.

grabbou avatar Feb 28 '17 09:02 grabbou

@dasmikko I completely forgot last night.

What I have is something like this in my splash screen class:

reset = (route) => {
  let resetAction = NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({ routeName: route})
    ]
  });
  this.props.navigation.dispatch(resetAction);
}

I have to say though that I believe that what @grabbou suggested might be a better solution.

BenMcH avatar Feb 28 '17 14:02 BenMcH

@grabbou

Params during render is a valid usecase. Even if @dasmikko's very specific use case is solved, it doesn't address the main issue. I changed the title so we can track this feature, because it's pretty important.

satya164 avatar Mar 01 '17 12:03 satya164

I feel it would also solve a couple of other issues we have spotted, esp. with Tab navigator.

grabbou avatar Mar 01 '17 13:03 grabbou

I think we could deprecate screenProps too once we have functionality to pass initialParams during render.

satya164 avatar Mar 01 '17 13:03 satya164

@grabbou

Alternatively, you can have two navigators and if statement in render

Are you able to provide sample code on how to do this? I tried following the example given by @jeffreymendez1993 but I get an error as it's my initial screen and there is no value for const { navigate } = this.props.navigation;

MrHubble avatar Mar 21 '17 01:03 MrHubble

Any progress on this?

f0rr0 avatar Mar 25 '17 03:03 f0rr0

is this feature request try to figure out the problem that react-navigation lib cant receive the props from native ? my issue is that, in hybrid App i try to receive the props that deliver from native App, but i cant receive any props. if so, is any progress on this ? if not, how can i receive the props deliver from native App. thanks.

woowalker avatar Mar 29 '17 08:03 woowalker

Is there a fix in works for this long-standing feature request? I have scenario where:

  • The first screen (initialRouteName) depends on props ( I have taken the route of creating 2 navigators with different initialRoute)

  • If one of the 2 navigators is selected, I need to pass a dynamic initialRouteParams, that is residing in the this.props of the component launching the navigator

Appreciate insight on how to address the second bullet point. Let me know if I can provide any additional information that can help with the creation of this feature

shrutic avatar May 22 '17 22:05 shrutic

@sata Pass initalRouteName as a props still not work now,so I have to define two or more StackNavigators as an alternative solution for conditional choice. Thanks for your job anyways

WeIio avatar May 24 '17 03:05 WeIio