【问题标题】:React-Native Error - "[TypeError: undefined is not an object (evaluating '_this2._OnAlert(count)')]"React-Native 错误 - “[TypeError: undefined is not an object (evalating '_this2._OnAlert(count)')]”
【发布时间】:2017-11-13 09:00:41
【问题描述】:

我正在尝试构建一个示例应用程序,它会弹出一个警报,显示 API 响应中的对象数量。警报弹出一次,但之后,我收到此错误

[TypeError: undefined is not an object (evaluating '_this2._OnAlert(count)')]

这是我的代码

import React from 'react';
import { View, Button, Alert } from 'react-native';
import ProductListingItem from './ProductListingItem';

export default class ProductDetailedListingPage extends React.Component {
  constructor() {
    super();

 }

 _OnAlert(count){
    console.log('_onAlert');
    console.log('count = '+ count);
    Alert.alert(
      'API call success',
      count+' objects',
      [
        {text:'done',onPress: () => { }}
      ]
    );
  }

 _getResponseFromApi(){
    console.log('_getREsponseFromApi called');
    var myRequest = new Request('http://mysampleapi.com:8082/listProducts',);
    myRequest.method = 'GET';
    return fetch(myRequest)
      .then((response)=> response.json())
      .then((responseJson) => {
        var data = responseJson.error;
        console.log('data = '+data);
        /*console.log(responseJson);*/
        var count = Object.keys(responseJson).length;
         count = '2';
         console.log('count in _getResponseFromApi = '+count);
        this._OnAlert(count);
        return responseJson;
      })
      .catch((error) => {
        console.log(error);
      });
  }

 componentDidMount () {
    console.log('componentDidMount');
    this._getResponseFromApi();
  }
  componentWillMount () {
    console.log('componentWillMount');
  }

 render(){
    console.log('render');
    return(
      <View style = {{
        flex:1,
        flexDirection:'column',
      }}>
      <Button style ={{flex:1}}
        onPress ={this._getResponseFromApi}
        title ="Call"
        />

     </View>
    );
  }
}

问题在于 _OnAlert() ,它在 componentDidMount() 首次运行没有问题并显示警报对话框,但在 onPress() 的实例中它给了我上面提到的错误。谈到 React 和 Javascript,尤其是 JSX,我是个菜鸟。我该如何解决这个问题?

【问题讨论】:

  • this._OnAlert(count).bind()中删除bind()
  • 试过了,现在错误是 [TypeError: undefined is not a function (evalating '_this2._OnAlert(count)')]
  • 我也在问题中更改它,以防万一。
  • onPress ={this._getResponseFromApi} 更改为onPress ={this._getResponseFromApi.bind(this)} 或将this._getResponseFromApi=this._getResponseFromApi.bind(this) 添加到构造函数中。
  • 酷,这也有效。只是出于好奇,为什么从componentDidMount() 调用OnAlert 时它会起作用?另外,为什么绑定OnAlert()this 不起作用??

标签: javascript reactjs react-native react-native-android jsx


【解决方案1】:

您应该更改您的构造函数并将方法“_getResponseFromApi”绑定到此处的范围,

constructor() {
super();
  this._getResponseFromApi = this._getResponseFromApi.bind(this);
}

【讨论】:

  • 嘿,谢谢,效果很好。你能告诉我是什么导致了这个错误,以及那个修复程序究竟做了什么来修复它?非常感谢,我已经坐了很长时间了。
  • 实际上我们是在使用 bind() 方法设置函数必须执行的范围。所以'this'是包含整个类范围的关键字。因此,要访问该范围内的任何内容,您需要访问该范围(由 .bind(this) 完成)
  • 据我了解,当通过按钮单击调用该方法时,它变成了一个函数调用,因此我们必须绑定该方法以使指针保持不变。而当从 `componentWillMount()' 调用方法时,它就像一个常规的方法调用一样工作..
猜你喜欢
  • 2016-07-30
  • 2020-08-31
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多