reactjs - Flow (React Native) 使用 'this.state' 时出现错误

标签 reactjs react-native flowtype

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

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

这是有问题的代码(尽管它也发生在其他地方):

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

任何帮助将不胜感激 =)

最佳答案

您需要为状态属性定义一个类型才能使用它。

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

关于reactjs - Flow (React Native) 使用 'this.state' 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860349/

相关文章:

javascript - 流程(推断错误): Cannot get 'object[key]' because an index signature declaring the expected key/value type is missing in 'Class'

javascript - 如何使用 Flow 键入默认导出?

reactjs - React - 使用 JSX 从 ES6 模板文字动态创建标签

react-native - 单击 native react 时如何更改同一屏幕上的内容

ios - 如何确保提交给 parse.com 的非登录用户数据仅来自移动应用

react-native - 用 Pnpm react Native Expo?

reactjs - 从 DOM 中移除 React 组件

javascript - Reactjs/Semantic Ui 在 map 中添加 if 条件

reactjs - 创建 react "development"构建

vue.js - 如何使用 flowtype 以外的 React$Element 类型键入 JSX?