Flow(React Native)给我使用'this.state'的错误 [英] Flow (React Native) is giving me errors for using 'this.state'

查看:66
本文介绍了Flow(React Native)给我使用'this.state'的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在代码中使用this.state时,Flow都会给我以下错误:

Flow is giving me the following error whenever I try to use this.state in my code:

对象文字: 此类型与 不明确的.您是否忘了声明标识符Component的类型参数State?:

object literal: This type is incompatible with undefined. Did you forget to declare type parameter State of identifier Component?:

这是令人讨厌的代码(尽管它也发生在其他地方):

Here is the offending code (though it happens elsewhere too):

class ExpandingCell extends Component {
    constructor(props) {
    super(props);
    this.state = {
        isExpanded: false
    };
}

任何帮助将不胜感激=)

Any help would be much appreciated =)

推荐答案

您需要为state属性定义一种类型才能使用它.

You need to define a type for the state property in order to use it.

class ComponentA extends Component {
    state: {
        isExpanded: Boolean
    };
    constructor(props) {
        super(props);
        this.state = {
            isExpanded: false
        };
    }
}

这篇关于Flow(React Native)给我使用'this.state'的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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