12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

flowtypeでのReact.Componentの型パラメータ

Last updated at Posted at 2016-03-25

2017年9月23日時点で、最新版の型定義を持ってくれば、次のような定義になります。

React.Component<Props, State = void>

DefinitelyTypedにある定義と同様の形式になっています。もはや迷うことはなくなったでしょう。

出典

DefaultProps を除去しているコミット
https://github.com/facebook/flow/commit/20a5d7dbf484699b47008656583b57e6016cfa0b#diff-5ca8a047db3f6ee8d65a46bba4471236R29

該当行
https://github.com/facebook/flow/blob/20a5d7dbf484699b47008656583b57e6016cfa0b/lib/react.js#L29


以下の内容は古くなっています

React.Component<DefaultProps, Props, State> {}

flowのDocsのReactのページには今のところ明記されていない。
(<void, Props<...>, State<...>>となっていて最初が不明)

TypeScript(DefinitelyTypedにあるd.ts)の場合は<Props, State>になっているので、移行の際は注意が必要。

出典

// @flow
import React from 'react';

type State = {count: number};
type Props = {count: ?number};
type DefaultProps = Props;

class Counter extends React.Component<DefaultProps, Props, State> {
  state: State; // WTF

  constructor(props: Props, context: any) {
    super(props, context);
    this.state = {count: 0};
  }
  static defaultProps = {count: 0};
}
12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?