Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
7
Help us understand the problem. What is going on with this article?
@berlysia

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

More than 3 years have passed since last update.

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};
}
7
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
berlysia
dwango
Born in the net, Connected by the net.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
7
Help us understand the problem. What is going on with this article?