Reactjs undefined不是一个函数(计算';this.setstate(

Reactjs undefined不是一个函数(计算';this.setstate(,reactjs,react-native,axios,Reactjs,React Native,Axios,我是react本地初学者。我已经使用“axios”npm包从API中为fech数据编写了一个代码。当我正常运行时,但当它是get data时,它是抛出错误 undefined不是一个函数(评估'this.setstate'( 我怀疑这个绑定有问题,但我不知道如何修复它。请帮助我解决这个错误 下面是我的代码: import React, {Component} from 'react'; import {Platform, StyleSheet, Text, View, ListView} fro

我是react本地初学者。我已经使用“axios”npm包从API中为fech数据编写了一个代码。当我正常运行时,但当它是get data时,它是抛出错误

undefined不是一个函数(评估'this.setstate'(

我怀疑这个绑定有问题,但我不知道如何修复它。请帮助我解决这个错误

下面是我的代码:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, ListView} from 'react-native';
import axios from 'axios';

export default class App extends Component {

  constructor(props){
    super(props);
    this.state = {
      isolate: true,
      movies: []
    }

  }

  componentWillMount(){
    axios.get('https://facebook.github.io/react-native/movies.json')
    .then(function (response){
      var moviesList = new ListView.DataSource({
        rowHasChanged: (r1,r2) => r1 !== r2
      });
      this.setState({
        isolate: false,
        movies: moviesList.cloneWithRows(response.data.movies)
      });
    }).catch(function(error){
alert(error);
    });
  }
  render() {
if(this.state.isolate){
  return (<View>

    <Text>Loading....</Text>
  </View>);
}

    return (
      <View style={styles.container}>
        <ListView 
        dataSource={this.state.movies}
        renderRow={
          (rowData) => <Text> {rowData.title} </Text>
        }
        >
        </ListView>
      </View>
    );
  }
}
import React,{Component}来自'React';
从“react native”导入{平台、样式表、文本、视图、列表视图};
从“axios”导入axios;
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
是的,
电影:[]
}
}
组件willmount(){
axios.get()https://facebook.github.io/react-native/movies.json')
.然后(功能(响应){
var moviesList=new ListView.DataSource({
行已更改:(r1,r2)=>r1!==r2
});
这是我的国家({
孤立:假,
电影:moviesList.cloneWithRows(response.data.movies)
});
}).catch(函数(错误){
警报(错误);
});
}
render(){
if(this.state.isolate){
返回(
加载。。。。
);
}
返回(
{rowData.title}
}
>
);
}
}

提前感谢。

您应该使用箭头函数作为回调函数:

axios.get('https://facebook.github.io/react-native/movies.json')
    .then(response => {
        // ...
    }

有关更多详细信息,请阅读以了解此的工作原理。

您应该使用箭头函数作为回调函数:

axios.get('https://facebook.github.io/react-native/movies.json')
    .then(response => {
        // ...
    }

有关更多详细信息,请阅读以了解
的工作原理。

您的
上下文在
内部是未知的,然后通过ajax调用
。 要么使用箭头函数传递
this
的上下文,要么在ajax调用之前定义一个变量
var self=this
,并在回调中使用它,如
self.setState…

componentWillMount(){
    axios.get('https://facebook.github.io/react-native/movies.json')
    .then((response) =>{
      var moviesList = new ListView.DataSource({
        rowHasChanged: (r1,r2) => r1 !== r2
      });
      this.setState({
        isolate: false,
        movies: moviesList.cloneWithRows(response.data.movies)
      });
    }).catch(function(error){
alert(error);
    });
  }

您的
这个
上下文在ajax调用的
then
中是未知的。 要么使用箭头函数传递
this
的上下文,要么在ajax调用之前定义一个变量
var self=this
,并在回调中使用它,如
self.setState…

componentWillMount(){
    axios.get('https://facebook.github.io/react-native/movies.json')
    .then((response) =>{
      var moviesList = new ListView.DataSource({
        rowHasChanged: (r1,r2) => r1 !== r2
      });
      this.setState({
        isolate: false,
        movies: moviesList.cloneWithRows(response.data.movies)
      });
    }).catch(function(error){
alert(error);
    });
  }

关于您的错误,这是因为您试图在错误的上下文中访问this。解决方法是使用箭头函数将正确的上下文绑定到this关键字。
我还建议您使用componentDidMount生命周期方法获取数据,使其成为:

componentDidMount() {
  axios.get('https://facebook.github.io/react-native/movies.json')
    .then(response => {
      var moviesList = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2
      });
      this.setState({
        isolate: false,
        movies: moviesList.cloneWithRows(response.data.movies)
      });
    }).catch(function(error) {
      alert(error);
    });
}

关于错误,这是因为您试图在错误的上下文中访问this的权限。解决方案是使用箭头函数将正确的上下文绑定到this关键字。
我还建议您使用componentDidMount生命周期方法获取数据,使其成为:

componentDidMount() {
  axios.get('https://facebook.github.io/react-native/movies.json')
    .then(response => {
      var moviesList = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2
      });
      this.setState({
        isolate: false,
        movies: moviesList.cloneWithRows(response.data.movies)
      });
    }).catch(function(error) {
      alert(error);
    });
}

then
callback应该是一个箭头来获取词汇信息,
then
callback应该是一个箭头来获取词汇信息,