-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set initialRouteName
and initialRouteParams
based on props
#458
Comments
Hey, Assuming your static knowledge is: const isLoggedIn = true; you can just use Otherwise, you can:
|
You can't set |
initialRouteName
and initialRouteParams
based on props
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) |
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 ***@***.***> 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
<#458 (comment)>,
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. |
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. |
I feel it would also solve a couple of other issues we have spotted, esp. with Tab navigator. |
I think we could deprecate |
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 |
Any progress on this? |
is this feature request try to figure out the problem that react-navigation lib can |
Is there a fix in works for this long-standing feature request? I have scenario where:
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 |
Any progress on this? |
Need this! |
I naively thought I could do this :(
|
@grabbou Regarding your comment of using if statements in the render function, I must disagree that it is satisfactory. By using conditional rendering, screen transitions are lost, which results in UI ambiguity for the user. I have attempted to wrap the elements in AnimatedAPI, but transitions are noticeably choppy for some reason. It seems to me that if StackNavigators are designed to be nested, they ought to have ways to dynamically set the initial route or else it becomes self-defeating. Would love to see this implemented when you guys have a chance. |
@richardgirges are you using redux to manage your nav state? |
@stantoncbradley - Thank you so much for taking the time to create a working example. I'm digging into your gist. I'll update you on my findings soon. |
@stantoncbradley I haven't integrated redux with react navigation - but my app is making heavy use of redux anyway so I wouldn't mind doing so if it would help resolve this issue. |
we haven't had to nest routers yet, but we've found moving our nav state to redux to be easier to work with since it abstracts the nav state out of the navigator. Redux should be able to persist your the nested nav states regardless what is being rendered |
@stantoncbradley - i've been busy but i took time to dig into the gist and test it in a real scenario. Your solution actually works pretty well. The only thing i noticed was in InitialRouteExample/Navigation.js
Apart from that, its a great solution and i highly recommend others to follow the pattern. |
Pinging OP @dasmikko to know if this issue can be closed, since the solution proposed by @stantoncbradley seems to be working fine for multiple users. Maybe @stantoncbradley can you do a small PR to add your example or an explanation to the docs? 😇 |
@kelset I think that @stantoncbradley's solution is clearly the best approach but I would appreciate this issue staying open until there's a more official solution - one that can work out of the box for both redux and non-redux integrated react-navigation apps. In addition to that, it feels like a lot of overhead that one must take on just to handle dynamic initial routes (including the need to re-define any components that can be initial routes in a second place, after already defining them as routes in your navigators). Obviously this can fall in priority now that there's a "good enough" solution - it's definitely good enough for me to keep going. But I fear that closing issues when there are only workarounds in place is what has ultimately depleted the quality of react-navigation. I say that a concerned citizen who has placed his bets on the future of react-navigation, it's not meant to be taken as criticism. If people disagree, then close away. Perhaps I'm misunderstanding the goal behind react-navigation's issue-triaging strategies. |
Hey @richardgirges thanks for your reply; I'll keep this issue open, but as you can see in #2585 probably it won't be implemented for v1.0. That said we are always trying to review each and every PR so it's not like if somebody provides a PR with this feature we will not merge it ;) |
Thanks @kelset! I have noticed that the open PRs have been slowly depleting. I may even take a crack at this one myself sometime soon, just started getting familiar with the react navigation codebase :) |
@kelset This can be closed, it works for me too. 👍 |
I appreciate your attitude @richardgirges :) How about starting with a new issue that follows the issue template for the feature request you have been writing about? After that you can open a PR and start working on it :) Don't worry about "not being too familiar" with the codebase, or even deadlines, it's all about you also learning while trying :) I'll use myself as an example ;) Look here: react-native-community/directory#50 (and I'll close this since OP agreed on it) |
My Full solution |
how to solve this issue without redux?? |
@danna88 I don't use redux in my solution |
@amorenew Thank you. I see you are using the InitialScreen, used to jump to other pages, I want to do the function is automatically Login, can I use Login page as InitialScreen? In addition, I used the react-native-splash-screen, but I encountered a problem: I do autoLogin in componentWillMount, then I closed Splash in componentDidMount, but it always render LoginScreen first,then navigate to other pages。 |
@danna88 I use splash without a library just with timer,
which renders nothing so the required screen only will be visible to the user |
@amorenew thank you very much! I 'll try it ! |
I use a splash screen and set the initial stack to render. I set up the configuration of my app during the initial launch and when it is completed the router/Stack is returned. |
So basically redirecting user to the Home screen in nested navigator scenario, I did this little trick. As react-navigation does not provide a way to set the Here is our Navigation file with Redux integration, which will show you the nesting of various navigators
Now we have done a little bit tweak in the first App.js file which will run just after botting the React native application from index.ios.js/index.android.js/index.js file. We just set a state
In |
easy as pie
|
@eugenecp this will only evaluate 'signedIn' once, right? How does this get updated once the user signs in/out? Wouldn't it have to rebuild the whole stackNavigator? |
Hi, I am using react-native tab navigator for two tabs, account and profile i want if user is logged in then only it will show the account and profile screen otherwise it will go to login screen. Here I have given onPress condition but how to check user is logged or not in below code... This is my code index.js import React, { Component,AsyncStorage } from "react"; import styles from "../../css/style"; export default (MainScreenNavigator = TabNavigator( {
} |
A workaround for that is to wrap the navigation into a class, get a ref on it, and dispatch a reset action on mount. class WrappedNavigation extends Component {
componentDidMount() {
this.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({routeName: this.props.initialRouteName})
]
}));
}
dispatch = (...args) => this.myNav.dispatch(...args);
return <MyNav ref={c => this.myNav = c}/>;
} Your static initial screen could render nothing ( |
|
If I want to keep the transition of react-navigation with dynamic |
@wellyshen may help #2683 |
if you need to set the that said, if you think that it is important to support these options as props then please feel free to thumbs up this rfc issue: react-navigation/rfcs#23. and if you really care about it a lot, please open a proper rfc from the rfc template! thanks! |
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.
The text was updated successfully, but these errors were encountered: