你的例子有点含糊,所以我尽量解释清楚。
有两种不同的方式将属性传递给屏幕(redux 实现除外)。
您可以通过将params 参数传递给屏幕来将参数传递给导航屏幕。
navigation.navigate({routeName, params, action, key}) 或
navigation.navigate(routeName, params, action)
routeName - 已在应用路由器某处注册的目的地 routeName
params - 合并到目标路由的参数
action -(高级)如果屏幕是导航器,则在子路由器中运行的子操作。有关完整列表,请参阅 Actions Doc
支持的操作。
key - 导航到哪个路径的可选标识符。如果它已经存在,则导航回该路线
示例
this.props.navigate('Profile', { name: 'Brent' })
您可以将全局参数传递给导航,该参数可用于该导航的每个屏幕。
screenProps - 将额外的选项传递给子屏幕
示例
const SomeStack = createStackNavigator({
// config
});
<SomeStack
screenProps={/* this prop will get passed to the screen components as this.props.screenProps */}
/>
我创建了一个小型示例应用程序,我猜您正在尝试实现它。
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>
);
}
}