编程语言
首页 > 编程语言> > javascript – 如何将道具传递给React Navigation导航器中的组件?

javascript – 如何将道具传递给React Navigation导航器中的组件?

作者:互联网

我正在尝试将道具传递给一个已经通过调用包装的组件来创建…导航器调用,即

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

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

我正在试图弄清楚如何将参数传递给搜索组件中的Wines / Stores / etc.组件(上图).我已经read the docs了,显然这可以通过传递一个对象轻松完成navigation.navigate,但我不知道如何使用这个特定的方法.有人可以帮忙吗?

解决方法:

你的例子有点模糊,所以我尽量解释.

将属性传递给屏幕有两种不同的方法(redux实现除外).

1)navigate行动

您可以将参数传递到导航屏幕,并将params参数传递给屏幕.

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

样品

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

2)screenProps

您可以将全局参数传递给导航,该导航可在该导航的每个屏幕中使用.

screenProps – Pass down extra options to child screens

样品

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>
    );
  }
}

标签:javascript,reactjs,react-native,react-native-android,react-navigation
来源: https://codeday.me/bug/20190607/1194630.html