Flow (React Native) is giving me errors for using 'this.state'

Michael Campsall picture Michael Campsall · Apr 26, 2016 · Viewed 14.9k times · Source

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

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 =)

Answer

edvinerikson picture edvinerikson · Apr 26, 2016

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