React Native - 从PassProps Navigator中检索值

时间:2017-04-07 22:06:27

标签: reactjs react-native

如何从新路线上的passProps获取参数?来自' Home'单身'确认值被解析而不确定如何在single.js上检索它们?

//index.ios.js
class AwesomeNativeBase2 extends Component {

  renderScene(route, navigator) {
    if(route.name == 'single') {
      return <Single navigator={navigator} />
    }
    if(route.name == 'home') {
      return <Home navigator={navigator} {...route.passProps} />
    }
    if(route.name == 'update') {
      return <Update navigator={navigator} {...route.passProps} />
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <Navigator
          initialRoute={{name: 'home'}}
          renderScene={this.renderScene.bind(this)}
        />
      </View>
    );
  }
}

//home.js
class Home extends Component {
  navigate(routeName, property) {
    this.props.navigator.push({
      name: routeName,
      passProps: {
        id: property
      }
    });
  }

//single.js
class Single extends Component {
  render() {
    console.log(this.props.navigator.pop());

1 个答案:

答案 0 :(得分:0)

正如其他路线所做的那样,按以下方式添加

  renderScene(route, navigator) {
    if(route.name == 'single') {
      return <Single navigator={navigator} {...route.passProps}/>
    }
   ....
  }

Single组件内,访问passProps的属性this.props.id