- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Setting screen specific props / params #740
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
Comments
You can work around this with a helper factory function. function createComponent(instance, props) {
return () => React.createElement(instance, props);
}
const AuthFlows = StackNavigator({
EnterInvite: {
screen: createComponent(EnterInvite, myProps),
}
}); I hope this helps. |
Thanks @jbovenschen this seems to partially work - but that component doesn't seem to receive the navigation prop now. Any ideas? |
import merge from 'lodash/fp/merge'
function createComponent(instance, ownProps) {
return (props) => React.createElement(instance, merge([ownProps, props], {});
} This will give you the props derived from the higher order function / higher order component created with the createComponent also it works nearly the same as the createFactory function within react which will be removed in the future (Could not find the source of that...). Hope this helps :) |
Home: { screen: props => <Home {...props} {...myProps} /> } |
thanks @jbovenschen & @satya164! - Satya's solution seems a little simpler, and is working for me so far - thanks again! |
I was running into this same issue, and the answers above were really helpful! Just as an FYI, when I wrapped my screen component in a function, as described above, my navigationOptions within the actual component (e.g., screen title) were not rendering. I set the navigationOptions within the navigator instead, and that solved the problem for me. |
Ditto, the static navigationOptions for the component seem to be ignored. @satya164 any idea how to use the component's navigationOptions? |
Ah, it has to do with hoisting the statics, but if I hoist the statics, (via import hoist from 'hoist-non-react-statics';
const DrawerNavigator = RNDrawerNavigator({
DisputeForm: {screen: hoist((props) => (<Link {...props} screenProps={{url: 'test.com'}}/>), Link)},
}) |
Inspired by this comment (#235 (comment)), I did something like this: const links = {
ContactForm: {
url: 'https://www.example.com/contact',
label: 'Contact Us',
},
Blog: {
url: 'https://blog.example.com',
label: 'Blog',
},
}
const DrawerNavigator = RNDrawerNavigator(
{
Home: {screen: TabNavigator},
ContactForm: {screen: DrawerLink},
Blog: {screen: DrawerLink},
},
{
navigationOptions(props) {
return {
...props.navigationOptions,
links,
};
},
},
);
// in DrawerLink.js
// CustomDrawerItem just renders a label and runs the onPress function
DrawerLink.navigationOptions = (props) => {
const {navigation: {state: {routeName}}, navigationOptions, screenProps} = props;
const {links} = navigationOptions;
return {
drawerLabel: ({tintColor, focused}) => {
return (
<CustomDrawerItem
focused={focused}
tintColor={tintColor}
onPress={() => {
WebBrowser.openBrowserAsync(
links[routeName].url,
);
}}
label={links[routeName].label}
/>
);
},
};
}; |
@satya164 It only works if |
It should be dynamic and we shouldn't have to create smart components, have to use NavigatorIOS instead |
@JulianKingman I can reproduce the issue (that the navigationOption is being ignored) but I do not understand how you're fixing it (I mean I can see your code but I cannot really get what you're doing). Can you provide a quick explanation? 🙏 😄 |
AS mentioned above, it has to do with static hoisting. Static properties (duh as navigation options) do not get automatically hoisted. Anyway, my solution is using navigationOptions as a function that returns the built in options (props.navigationOptions), along with my additional properties. Does that answer your question? |
This is what I came up with.
|
A technique that I used to pass a defaultParam to a route:
|
ScreenProps are immutable once they are set and sent once, and therefore you cannot filter them out. Perhaps you can dispatch params once you have them available. This worked beautifully for me. Courtesy of perusing the docs: https://reactnavigation.org/docs/en/navigation-actions.html#setparams It even guarantees to change the state.
|
Here is a working example of how to set |
Hi @rahulgi , This is actually works for me. I used this method as follows,
Inside the 'createDrawerNavigator'. Most people's solutions is not worked for me, But yours worked for me. If anyone has any other ideas efficient than this, please suggest me. Thanks. |
For params you can use the property params: const AuthFlows = StackNavigator({
EnterInvite: {
screen: EnterInvite,
params: {
param1: value
},
}
}); |
Is there a way to do this in the StackNavigator that I'm not seeing?
I would like to do something like:
The text was updated successfully, but these errors were encountered: