Flow: Can't define React state in React.Component sub-subclasses

Created on 19 May 2016  路  4Comments  路  Source: facebook/flow

This code:

/**
 * @flow
 */

import React from 'react';
import {Text, View} from 'react-native';


class ParentClass extends React.Component {
  textRender() {
    throw ('Not Implemented!');
  }

  render() {
    return <View>Parent: {this.textRender()}</View>;
  }
}

class BasicSubclass extends ParentClass {
  textRender() {
    return <Text>Basic</Text>;
  }
}

class SubclassWithState extends ParentClass {
  state: {
    data: number,
  };

  constructor(props: Object) {
    super(props);
    this.state = {
      data: -1,
    };
  }

  textRender() {
    return <Text>State: {this.state.data}</Text>;
  }
}

Gives the following errors:

test.js:9
  9: class ParentClass extends React.Component {
                               ^^^^^^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of property `Component`?. This type is incompatible with
 31:   state: {
              ^ object type

test.js:31
 31:   state: {
              ^ object type. This type is incompatible with
  9: class ParentClass extends React.Component {
                               ^^^^^^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of property `Component`?
duplicate bug react

Most helpful comment

this simplified example does not work neither

/* @flow */

class ParentClass {
  state: {
    foo: string
  }
}

class Subclass extends ParentClass {
  state: {
    foo: string,
    bar: string
  }
}

All 4 comments

@thejameskyle Is this intentional?

this simplified example does not work neither

/* @flow */

class ParentClass {
  state: {
    foo: string
  }
}

class Subclass extends ParentClass {
  state: {
    foo: string,
    bar: string
  }
}

I met this problem too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glenjamin picture glenjamin  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

doberkofler picture doberkofler  路  3Comments

Beingbook picture Beingbook  路  3Comments

funtaps picture funtaps  路  3Comments