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
How to pass initialProps
to a StackNavigator screen?
#876
Comments
They are being passed to which passes them inside render() to createNavigator render() View Then they're passed to CardStack at which point the values are in But the render() only receives the default For now I can fork the implementation and add just add the specific variable I want to be passed, however it'd be great to figure out how to handle the initialProps instead of having them mushed together with the rest of the navigation props. PS: looking through the source was actually nice, lit code. |
Ok, got a temporary hack in my fork. I check for the route of the component that I want to pass the prop to, and modify the navigation state params. It would be nice to have this supported by default so I'll try thinking of a way to PR this. In any case, I'd love to hear your ideas on how this could be added. |
So, the component won't re-render on After all this I'm forced to use duplicate AppRegistry with same components but different names. Same for UIViewControllers and the RCTRootViews that are instantiating the modules. It's not the worst but it could've been cleaner.
|
It sounds like you've found a workaround. To my understanding, this is more of a RN bug than a problem with the nav library. RN makes it easy to provide initial props from native to the root react component, but it is not easy to update the props, so you'd need a better abstraction for that. |
@ericvicenti What? the problem is clearly with the library - I still have to utilize a complete wack hack. Please re-open. The library is messing with basic React Native functionality; my workaround is a total hack of something that should be supported by default. Also the navigation state should be kept up to date with latest app initialProps. I need not a better abstraction for updating the props, please read the very first issue comment one more time. Once again, lib issues:
|
Ping @ericvicenti |
1 similar comment
Ping @ericvicenti |
Hi, I'm not sure why you're calling Have you tried using the const Foo = StackNavigator({
List: {
screen: List,
path: ':demoListType',
}, // ... paths
});
const List = ({ screenProps }) => <MyListView foo={screenProps.bar} />;
// first, you can render:
<Foo screenProps={{ bar: true }} />;
// then you can re-render the navigator and the new screen prop will flow down to MyListView:
<Foo screenProps={{ bar: false }} />; |
@ericvicenti calling
|
use screenProps initialProperties:@{@"screenProps":@{@"xxx":@"xxxx"}} |
As already mentioned by others, the way you are doing it:
is not going to work. You should instead:
|
@grabbou @ferologics I still can't access the I'm not sure if we're doing something wrong. But you have closed this and the other issue that I made regarding the same thing (#634). You seem to be really confident that this is doable so please let us know what your secret here to accessing Thanks! |
@grabbou @ferologics |
Okay, finally understood the approach you were taking and managed to get it working. To help others, here is a full solution: import React from 'react';
import {
AppRegistry,
Text,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class MyApp extends React.Component {
render() {
console.log('this.props in MyApp', this.props); // This will list the initialProps.
// StackNavigator **only** accepts a screenProps prop so we're passing
// initialProps through that.
return <Navigator screenProps={this.props} />;
}
}
class HomeScreen extends React.Component {
render() {
console.log('this.props in HomeScreen', this.props);
// This will output something like this:
// { screenProps: { ...your initialProps }, navigation: { ...StackNavigator stuff... } }
return <Text>My initialProps are {JSON.stringify(this.props.screenProps)}.</Text>;
}
}
const Navigator = StackNavigator({
Home: { screen: HomeScreen },
});
AppRegistry.registerComponent('MyApp', () => MyApp); Thanks and I hope that helps clarify for others, including @ferologics. |
Hey, after this issue, I decided that React Navigation is performing extremely poorly in my production app and I decided to go with native iOS navigation + a bunch of RCTRootViews. ((( why I decided to do so:
After testing with couple users they all concurred that the navigation is unlike the native one. Overall less responsive UI and there were subtle abovementioned animation differences that influenced the UX. ))) @grabbou and @ericvicenti are totally right, I should've used the My error was in expecting the StackNavigator to take the ^ Feedback to consider for the project collaborators and a lesson for me to read docs more thoroughly. Thanks for all your support! |
@joshuapinter I ❤️ U. |
@robertsimoes 🙏 |
Works perfectly ! Thanks @joshuapinter |
@ferologics what debugger are you using for react-native? |
@joshuapinter @grabbou can you guys please give a solution which works for react-navigation-3 in which you must set up your app container directly. |
Use this.props.screenProps , you can take it. |
Desired behaviour
I'm using one component to display different content based on the initialProps. Think list with 2 data sources that depend on initialProps.
I have a
RCTRootView
. I modify it'sappProperties
. Docs say that the component re-renders with newappProperties
inthis.props
on change.The console prints
But the screen doesn't receive any properties and the component doesn't re-render. I put a breakpoint inside
componentWillUpdate
to check. Any idea where the props are going and how to access them? They aren't being passed inside navigation state either.Workarounds
Lazy loaded component
I recall that there was a lazy way to load components
getScreen: () => ReactComponent
. It might also be too slow to lazy load the component, ideally it's pre-loaded b/c React Native can take some time to render.This works, but:
I'd like the
appProperties
be passed toprops
in the screen.Better would be if they're passed with navigation as state so that they can be used in navigationOptions for header titles, deep linking, etc.
The text was updated successfully, but these errors were encountered: