react-native TypeError:undefined不是对象(评估'this.props.navigator.push')

时间:2016-05-25 09:49:40

标签: android reactjs react-native

我正在尝试这个针对android的本机反馈教程: https://www.raywenderlich.com/126063/react-native-tutorial

我遇到了导航员的问题。第一个屏幕(SearchPage)工作正常,但是当尝试.push到下一个时它会给我以下错误:

发生了一些不好的事情TypeError:undefined不是一个对象(评估'this.props.navigator.push')

主:

render () {
 return(
  <Navigator
    initialRoute={{id: 'SearchPage'}}
    renderScene={this.renderScene.bind(this)}
    />
 );
}

renderScene(route, navigator) {
 switch (route.id) {
  case 'SearchPage':
   return (
    <SearchPage navigator={navigator} />
   );
   case 'TestScreen':
    return (
     <TestScreen navigator={this.props.navigator} />
   );}

SearchPage:

_executeQuery(query) {
 this.setState({ isLoading: true });
 fetch(query)
  .then(response => response.json())
  .then(json => this._handleResponse(json.response).bind(this))
  .catch(error =>
     this.setState({
      isLoading: false,
      message: 'Something bad happened ' + error
   }));
}

_handleResponse(response) {
  this.setState({ isLoading: false , message: '' });
  if (response.application_response_code.substr(0, 1) === '1') {
    this.props.navigator.push({
      id: 'TestScreen',
    });
  } else {
    this.setState({ message: 'Location not recognized; please try again.'});
  }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我认为这是一个范围问题。尝试使用es6胖箭:

renderScene = (route, navigator) => {
 switch (route.id) {
  case 'SearchPage':
   return (
    <SearchPage navigator={navigator} />
   );
   case 'TestScreen':
    return (
     <TestScreen navigator={navigator} />
   );
}

注意我已经通过导航器更改了TestScreen案例this.props.navigator。

_executeQuery = (query) => {
 this.setState({ isLoading: true });
 fetch(query)
  .then(response => response.json())
  .then(json => this._handleResponse(json.response))
  .catch(error =>
     this.setState({
      isLoading: false,
      message: 'Something bad happened ' + error
   }));
}

_handleResponse = (response) => {
  this.setState({ isLoading: false , message: '' });
  if (response.application_response_code.substr(0, 1) === '1') {
    this.props.navigator.push({
      id: 'TestScreen',
    });
  } else {
    this.setState({ message: 'Location not recognized; please try again.'});
  }
}