Michael Campsall Michael Campsall - 1 month ago 26
React JSX Question

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

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

You need to define a type for the state property before you can use it.

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