4

In my index.ios.js, I have the following:

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

and

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

then in my FirstPage.js, to navigate to the SecondPage.js:

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

Then in my SecondPage.js, it just renders the component in a . And in my ThirdPage.js:

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


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

Then in my FourthPage.js, I simply have:

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

I am getting the error from the ThirdPage.js, which says:

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

and I cannot seem to figure out why, because the this.props.navigator.push worked fine for both .replace and .push, but now I am getting this error. Any guidance or insight would be appreciated. Thank you in advance.

6
  • push is a function on arrays and this.props.navigator is an object. Commented Jul 14, 2016 at 16:41
  • @JohnS Navigator indeed has push facebook.github.io/react-native/docs/navigator.html Commented Jul 14, 2016 at 16:58
  • OP did you bind your _rowPressed method to the component in your constructor? Commented Jul 14, 2016 at 16:59
  • @JohnS As far as I know, this.props.navigator.push works on objects. Commented Jul 14, 2016 at 17:03
  • @azium Sorry but could you clarify on what you mean? This is what I have as my constructor: constructor(props){ super(props);} Commented Jul 14, 2016 at 17:07

1 Answer 1

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

you may just need to bind the Component

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it and no error but the method '_rowPressed*()' is somehow not being called, and I can't seem to figure out why... tried console.log inside _rowPressed() and it is not logging even though it is being called via onPress. What could be the issue?
try this this._rowPressed.bind(this)
Actually it works and calls the method and logs correctly, but now I am getting the following error: Cannot read property 'push' of undefined
navigator.props.push() try this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.