Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  127] [ 1]  / answers: 1 / hits: 28522  / 6 Years ago, sun, may 20, 2018, 12:00:00

I'm trying to pass props to a component that has been wrapped through a call to create...Navigator call, i.e.



// Search.js
const Navigator = createMaterialTopTabNavigator({
Wines,
Stores,
Vineyards,
Restaurants
});

// Somewhere in render()...
<Navigator />


I'm trying to figure out how to pass parameters to the Wines / Stores / etc. components from the Search component (above). I've read the docs and apparently this can be done easily with navigation.navigate by passing in an object, but I'm not sure how to do it with this particular method. Can someone please help?


More From » reactjs

 Answers
29

Your example is a bit vague so I try to explain as much as I can.



There is 2 different ways to pass properties to screens (except redux implementation).



1) navigate action



You can pass parameters to navigated screen with passing params argument to the screen.




navigation.navigate({routeName, params, action, key}) OR
navigation.navigate(routeName, params, action)



routeName - A destination routeName that has been registered somewhere in the app's router



params - Params to merge into the destination route



action - (advanced) The sub-action to run in the child router, if the screen is a navigator. See Actions Doc for a full list of
supported actions.



key - Optional identifier of what route to navigate to. Navigate back to this route, if it already exists




Sample



this.props.navigate('Profile', { name: 'Brent' })


2) screenProps



You can pass a global parameter to the navigation which can be available in every screen for that navigation.




screenProps - Pass down extra options to child screens




Sample



const SomeStack = createStackNavigator({
// config
});

<SomeStack
screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
/>


I have created a small sample app which I am guessing you are trying to achieve.



const Tab = ({name, searchValue}) => (
<View style={styles.tabContainer}>
<Text>{name}</Text>
<Text>{`Searching: ${searchValue || '...'}`}</Text>
</View>
);

const Wines = (props) => (<Tab name=Wines Page searchValue={props.screenProps.searchValue} />);
const Stores = (props) => (<Tab name=Stores Page searchValue={props.screenProps.searchValue} />);
const Vineyards = (props) => (<Tab name=Vineyards Page searchValue={props.screenProps.searchValue} />);
const Restaurants = (props) => (<Tab name=Restaurants Page searchValue={props.screenProps.searchValue} />);

const Navigator = createMaterialTopTabNavigator({
Wines,
Stores,
Vineyards,
Restaurants
});

export default class App extends Component {
state = {
text: ''
}
changeText = (text) => {
this.setState({text})
}
clearText = () => {
this.setState({text: ''})
}
render() {
return (
<View style={styles.container}>
<SearchBar
lightTheme
value={this.state.text}
onChangeText={this.changeText}
onClearText={this.clearText}
placeholder='Type Here...' />
<Navigator screenProps={{searchValue: this.state.text}} />
</View>
);
}
}

[#54393] Wednesday, May 16, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaidyn

Total Points: 633
Total Questions: 102
Total Answers: 100

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
2 Years ago
;