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
Params not being passed when navigating between Stacks (repro included) #143
Comments
I think you're running into this problem because your Have you tried replacing HeaderStack with SecondView when defining NoHeaderStack? ie:
|
@elliottjro What you suggest does work correctly. That doesn't accomplish the UX I'm looking for though. Is what I'm asking for something I shouldn't expect to work? |
@davideweaver hmm so what you need to do is somehow pass the params from |
hey @davideweaver try this:
Whats going on is -> we navigate to "HeaderStack" and then set a second "Navigate" action to be handled by the child router (which is the "HeaderStack" |
It's expected behaviour right now, but I agree that's it's confusing. cc @ericvicenti @skevy |
@elliottjro Thanks for the code. Now that I see it the docs make sense. For those following along, you would use the same pattern for navigating to a deeper screen as well. Here's the code if HeaderStack was placed into a TabNavigator...
That being said, the fact that navigate is smart enough to get to the correct screen without defining the additional actions, I think it would be great to have it pass the parameters through automatically too (unless I'm missing something). |
I still can't get this working. I have a Drawer inside a Stack, and I'm trying to navigate between two screens in the Drawer. const MainNavigator = DrawerNavigator({
Main: {screen: Main},
Notifications: {screen: Notifications},
});
const SplashStackNavigator = StackNavigator({
Login: {screen: Login},
MainNavigator: {screen: MainNavigator}
}, {
initialRouterName: 'Login',
headerMode: 'none',
}); I've tried: this.props.navigation.navigate('Main', {param: 'param'});
this.props.navigation.navigate('MainNavigator', {}, {
type: "Navigate",
routeName: "Main",
params: {param: 'param'},
}); And in all cases inside Main constructor: const navParams = this.props.navigation.state.params; is undefined. Thanks. |
@ilaif it looks like you're calling the same way I have it working. I haven't tried it with a Drawer Navigator though. That may behave differently than a TabsNavigator. The only other thing I see different, which shouldn't matter, is that I'm using destructuring when accessing params:
Maybe try dumping your navigation when the target page loads:
|
This workaround doesn't really make sense to me (nor can I even get it to work). To take an example which I imagine would be quite a common use case: say the main navigator of my app is a StackNavigator, with the main screen being a ListView of contacts. When I click a contact, I want to navigate to a screen with sliding tabs (TabNavigator), where each tab displays a different piece of info about that contact. With this nested navigate action workaround, the best I can hope for is that I can pass the contactId param to just one of the tabs. (Which would cause an error on the other tabs unless What I think would make a lot more sense here is if I could simply call |
I have the latest version and am still having this problem. Root navigator: const AppNavigator = StackNavigator({
MainDrawer: {screen: MainDrawer},
}, {
navigationOptions: {}
}); MainDrawer: const MainDrawer = DrawerNavigator({
Home: {screen: Home},
PastIncidents: {screen: PastIncidents},
NewIncident: {screen: NewIncident},
}, {
navigationOptions: {...},
drawerPosition: 'right',
contentOptions: {
style: {
},
},
}); on navigation.navigate('NewIncident', {incidentId}); The incidentId is not passed to the NewIncident component. Ideas? |
i have same problem |
Looks like this may get fixed by this PR: #969 |
Yeah, looks like this issue was a duplicate of the ones mentioned by the PR. That should be fixed in latest master. |
The linked issues seem different. |
You are right. I got confused by one of the commits that talks about Drawer #143 (comment). Drawer should work fine. The original issue is about navigating between |
The issues seems to be fixed for For example:
Running I dig a little bit in the code and found what seem to be the problem -
which return false - because tabId (which is the name of the TabNavigation itself is not matching any one of it's child screen names), and because it return false the code that set the params to the children is not executed. I don't know how to solve it, so if anyone can fix it please do. |
The workaround mentioned above does not fit my navigation structure as well. I have a main DrawerNavigator where each route is a StackNavigator. The StackNavigator has an initial route configured so when I route to the stack I get that screen. const DrawerNav = DrawerNavigator({
Home: { screen: HomeScreen, },
Document: { screen: DocumentStack}},
{
initialRouteName: 'Home'
}); const DocumentStack = StackNavigator({
List: { screen: ListScreen },
Details: { screen: DetailsScreen }
},{
initialRouteName: 'List',
}); When navigate to the stack and do an additional navigation to that (initial) screen, I get the screen with a back button - and when I press it, it goes back to the exact screen but without the parameter. navigate('Document', {},
{
type: "Navigate",
routeName: "List",
params: {list: 'orders'}
}) Screen shot after navigation: (The list page is second in navigation stack and has a back button) Screen shot after I press the back button: (The list name is empty - because no params) Is this going to be fixed |
+1. Also getting undefined params when navigating from one stack to a screen in a nested stack. this.props.navigation.navigate('parentStack', {}, {
type: "Navigate",
routeName: "childStack",
params: payload
}); param still undefined. Would be great if navigators passed params along. |
Any Solution to this ? Facing the same problem |
I have this issue too +1 |
This might help and work for me. On Screen1 I am passing the data while navigating to Screen2 like this: <TouchableHighlight On Screen 2, I am receiving this data which is "i" here, const {i} = this.props.navigation.state.params; I have constructor on both component and which is like this : constructor(props){ |
Thing is, I'm passing this param from one screen and trying to display it a few screens away in my stack nav and accessing the param value there gives me an undefined :( |
Hi , I am facing the same issue not being able to pass Params.
I am trying to navigate from |
Thanks for opening this one again @spencercarli ! |
Is there a solution to this? Tried all suggestions from above but nothing works. Im trying to pass props between two screens under DrawerNavigation. |
I logged every navigation state (everytime I navigate to other screen) and made my own react-navigation helper functions. // MyComponent.js
const renderHeader = (params) => <Header params={ params } />;
class MyComponent extends Component {
static navigationOptions = {
header: ({ navigation }) => renderHeaderWithNavigation(navigation)(renderHeader)
}
componentDidMount() {
const params = { ... };
setParamsToNavigation(this.props, params);
}
} // Router.js
export const navigateTo = (props, to, params = {}) => {
props.navigation.navigate(to, params);
};
export const setParamsToNavigation = (props, params) => {
props.navigation.setParams({ ...params });
};
export const renderHeaderWithNavigation = (navigation) => {
const params = getParamsFromNavigationState(navigation.state);
return (renderHeaderMethod) => {
return renderHeaderMethod(params);
};
};
// recursive function for nested navigator
export const getParamsFromNavigationState = (state) => {
if (hasPath(state, 'index')) {
const { index, routes } = state;
return getParamsFromNavigationState(routes[index]);
} else {
return hasPath(state, 'params') ? state.params : null;
}
}; And if I want to pass params to other screen as props, I used HOC export const mapNavigateParamsToProps = (WrappedComponent) => {
return class MappedComponent extends Component {
static navigationOptions = WrappedComponent.navigationOptions;
render() {
const { navigation } = this.props;
const { state: { params } } = navigation;
return <WrappedComponent { ...this.props } { ...params } />;
}
};
}; |
I have a similar problem
Now in page
When I goBack |
I would like to share a similar problem where I couldn't pass params to my DrawerNavigator, as well as my solution. This is a specific case but it might help somebody. BeforeStructure:
In Home Screen:
In MyMenu:
this.props.navigation.state.params is undefined. I figured that you can only send navigation props when navigating to a new screen - 'DrawerOpen' does not count for this. To solve this I had to change my structure a little bit, instead of having the DrawerNavigator as the root drawer, I have a StackNavigator as the root drawer. After
Now in Login screen I navigate to Drawer like this:
And in MyMenu (which is part of 'Drawer'):
It works! |
We have a similar setting as #143 (comment) What we found is the latest version |
I would like to share a work around for the problem I mentioned in my comment above, maybe it will help somebody: // my stack navigator
const DocumentStack = StackNavigator({
List: { screen: ListScreen },
Details: { screen: DetailsScreen }
},{
initialRouteName: 'List',
});
// create a wrapper component for stack navigator
// and pass params and rootNavigation via screenProps
// (solution!!!)
class DocumentsStackNav extends Component {
render() {
const { navigation } = this.props;
return <DocumentStack
screenProps={{
params: navigation.state.params,
rootNavigation: navigation
}}/>
}
// my drawer navigator - were 'Document' references the wrapper component
const MenuDrawer = DrawerNavigator({
Home: { screen: HomeScreen },
Document: { screen: DocumentsStackNav },
},
{
initialRouteName: 'Home',
})
// now we can simply navigate to DocumentsStack passing params
navigation.navigate('Document', params)
// and access them in the ListScreen (initialRoute of DocumentStack)!!!!
// we also have access to the DrawerNavigator's navigation
// if we would like to navigate to a different drawer item from here...
class ListScreen extends Component {
render() {
const { params, rootNavigation } = this.props.screenProps;
}
} This solution works great for me! No need for extra navigation action, and the most important, access params!!! |
I copied the exact code from the initial post into a Snack and it works as expected: https://snack.expo.io/HyhE2T38G I imagine this has been resolved, please create a new issue and follow the updated issue template if you're still having problems! :) |
@bartolkaruza I wrap my three stackNavigators in a TabNavigator. Then I pass a param from one of the stackNavigator through react-navigation: v1.0.0 |
@wellyshen I'm sure you meant to tag someone else :) |
Something very similar happens to me like @wellyshen, my provisional solution is to work with version react-navigation@1.0.0-beta.21 |
did the beta version allow you to pass params between nested navigators using state.props? @carlos-proogresa having trouble with this issue right now as well |
See I tell u guys! |
@sethnkruse, I feel my English because I'm using a translator. I hope I'm answering your question. I am currently passing parameters from one screen to another with the version I have discussed. A piece of code from my router.js file:
It all starts in RootStack and from Splash I navigate to HomeTabs by passing parameters. HomeTabs is a TabNavigator that contains HomeStack and finally Home is my screen where I consume these parameters. From SplashScreen the code I use to navigate and send parameters:
In HomeScreen I use them with:
And that's all, I hope it's understood. |
the issue I found with @carlos-proogresa method is that if you are using TabNavigator the screen does not go to that specific tab/stack but just another screen in its own stack. |
Still getting this when navigating from one StackNavigator to another, this is easily reproducible, so why is has the issue been closed? |
My params were not being passed and the state object did not contain the params object even though I set it. I tried everything. It was not working. Then I changed my StackNavigator and put my screen in the right StackNavigator and it works.
|
Has any found a solution to this issue? Have tried every solution in this thread and nothing's working for me. |
More than a year and still not fixed, wow! |
please create a new issue and follow the issue template, this one is old and before the current maintainers started working on it. i would be happy to look into this if you can do that, thanks! |
I use the following code to create an initial screen that does not have a NavBar but pushes on a stack that contains a floating NavBar. When navigating to the second stack, params are being passed. It doesn't appear that those params are being passed to the new screen. This can be seen in the attached screenshot. undefined should not be showing.
index.ios.js
Is this a bug, or is there another way to accomplish the same thing?
The text was updated successfully, but these errors were encountered: