反应本机setState不是函数 [英] React Native setState not a function

查看:84
本文介绍了反应本机setState不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么 this.setState 不是函数吗?

Can someone explain me why this.setState is not a function ?

我看不到为什么我的代码有错误

I do not see why my code has an error

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);
            });
    }
 }

谢谢

推荐答案

这是 lexical scope 问题.使用 arrow function .

.then((response) => {
    this.setState({ res: response });
})

这篇关于反应本机setState不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆