react-native icon indicating copy to clipboard operation
react-native copied to clipboard

ES6 and NavigatorIOS: message: Cannot read property 'navigator' of undefined

Open tonylinyy opened this issue 11 years ago • 2 comments

hi,I write the code like this,but report an error: message: Cannot read property 'navigator' of undefined and I try another way without ES6,everything ok. something wrong with ES6?

class MyView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  editMyData() {
    this.props.navigator.push({
      component: EditMyData,
      title: 'editMyData'
    })
  }

  render() {
    return (

      <ScrollView style={styles.container} showsVerticalScrollIndicator={false}>
        <View style={styles.header}>
          <View style={styles.user}>
            <Image style={styles.myImage} source={require('image!logo')}/>
            <Text>test</Text>
          </View>
          <TouchableHighlight onPress={this.editMyData}>

tonylinyy avatar May 02 '15 17:05 tonylinyy

When you create components as ES6 classes methods are not bound to instance automatically. Try this:

class MyView extends React.Component {
  constructor(props) {
    super(props);
    this.editMyData = this.editMyData.bind(this);
    this.state = {};
  }
...

vkurchatkin avatar May 02 '15 19:05 vkurchatkin

it works,thanks

tonylinyy avatar May 02 '15 19:05 tonylinyy