React Native, NavigatorIOS, undefined is not an object (evaluating 'this.props.navigator.push')

别等时光非礼了梦想. 提交于 2019-11-29 10:03:40

Since you're using ES6 you need to bind 'this'.

For example: onPress={this.onPress.bind(this)}

Edit: Yet another way that I've been using more recently is to use an arrow function on the function itself, since they will automatically bind the outside this.

onPress = () => {
  this.props.navigator.push({
    title: 'Routed!',
    component: Park
  });
};

You can bind the function to this in the constructor.

constructor(props) {
    super(props);
    this.onPress = this.onPress.bind(this);
}

Then use it when rendering without binding.

render() {
    return (
        <TouchableHighlight onPress={this.onPress} style={styles.button}>
            <Text>Welcome to the NavigatorIOS. Press here!</Text>
        </TouchableHighlight>
    );
}

This has the advantage of being able to be used multiple times and also clean's up your render method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!