【发布时间】:2018-06-29 09:43:55
【问题描述】:
谁能解释一下为什么 this.setState 不是函数?
我不明白为什么我的代码有错误
import React from 'react';
import axios from 'axios'
import { StyleSheet, Text, View , Image} from 'react-native';
export default class App extends React.Component {
constructor(){
super();
this.state = {res: []}
}
componentDidMount() {
axios.get('https://api.github.com/repos/torvalds/linux/commits')
.then(function (response) {
this.setState({res: response});
}).catch(function (error) {
console.log(error);
});
}
}
谢谢
【问题讨论】:
-
使用箭头函数包裹
this.setState({res: response});,将this带入箭头函数
标签: javascript reactjs function react-native setstate