javascript - 为什么我得到 'Cannot read property ' push' of undefined' (React Native)?

标签 javascript ios react-native reactjs

在我的 index.ios.js 中,我有以下内容:

  renderScene(route, navigator){
    return <route.component navigator={navigator} {...route.passProps}/>
  }

render() {
    return (
      <Navigator
        initialRoute={{name: 'FirstPage', component: FirstPage}}
        renderScene={this.renderScene}
      />
    );
  }

然后在我的 FirstPage.js 中导航到 SecondPage.js:

  _navigate(){
    this.props.navigator.replace({
      component: SecondPage,
      name: 'SecondPage'
    })
  }

然后在我的 SecondPage.js 中,它只是在 .在我的 ThirdPage.js 中:

  _rowPressed(){
    this.props.navigator.push({
      component: FourthPage,
      name: 'FourthPage',
    })
  }


  <TouchableHighlight
    onPress={() => this._rowPressed()}
  >
     <View>
          <Text>Go to FourthPage</Text>
     </View>
  </TouchableHighlight>

然后在我的 FourthPage.js 中,我只需要:

  <View style={styles.container}>
    This is FourthPage.js
  </View>

我从 ThirdPage.js 收到错误,它说:

Cannot read property 'push' of undefined in _rowPressed()

我似乎无法弄清楚为什么,因为 this.props.navigator.push 对于 .replace 和 .push 都工作正常,但现在我收到了这个错误。任何指导或见解将不胜感激。提前谢谢你。

最佳答案

<TouchableHighlight
    onPress={this._rowPressed.bind(this)}//add this
  >
     <View>
          <Text>Go to FourthPage</Text>
     </View>
  </TouchableHighlight>

你可能只需要绑定(bind)组件

关于javascript - 为什么我得到 'Cannot read property ' push' of undefined' (React Native)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38379656/

相关文章:

javascript - js代码获取动态生成的div ID

ios - SKShapeNode 不绘制 UIBezierPath

javascript - 是的,针对 punycode 和西里尔字母的域名验证

javascript - 为所有组件 React-Native 设置不 header

react-native - React Native 检查取消选中不适用于自定义图标

javascript - .prop ('selected' ) 不工作,按钮在 jquery 中一直被禁用

javascript - JQuery 表单插件向 Node.js 发送空数据?

javascript - 通过 Filesystem API 导出文件时 Chrome 崩溃

javascript 变量替换 this.refresh , window.refresh

IOS:AVAudioSession 不工作