Set `initialRouteName` and `initialRouteParams` based on props
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.
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:
- switch screen in render()
- switch navigator in render()
You can't set initialRouteName and initialRouteParams based on props right now, but I think we need this.
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 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.
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 .
Now I understand.
Looking forward for the new feature! :)
@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
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 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.
Looking forward to see the example.
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.
@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.
@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.
I feel it would also solve a couple of other issues we have spotted, esp. with Tab navigator.
I think we could deprecate screenProps too once we have functionality to pass initialParams during render.
@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;
Any progress on this?
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.
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
@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