Components

Learn how to type React class components and stateless functional components with Flow

Search docs

  1.  

Adding Flow types to your React components is incredibly powerful. After typing your component, Flow will statically ensure that you are using the component in the way it was designed to be used.

Early in React’s history the library provided PropTypes which performed basic runtime checks. Flow is much more powerful as it can tell you when you are misusing a component without running your code.

There are some Babel plugins which will generate PropTypes from Flow types such as babel-plugin-react-flow-props-to-prop-types if you want both static and runtime checks.

Class Components

Before we show how to type a React class component with Flow, let us first show how you would write a React class component without Flow but with React’s prop types. You would extend React.Component and add a static propTypes property.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. class MyComponent extends React.Component {
  4. static propTypes = {
  5. foo: PropTypes.number.isRequired,
  6. bar: PropTypes.string,
  7. };
  8. render() {
  9. return <div>{this.props.bar}</div>;
  10. }
  11. }
 

Now, let’s Flowify the component we just wrote:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  1. import * as React from 'react';
  2. type Props = {
  3. foo: number,
  4. bar?: string,
  5. };
  6. class MyComponent extends React.Component<Props> {
  7. render() {
  8. this.props.doesNotExist; // Error! You did not define a `doesNotExist` prop.
  9. return <div>{this.props.bar}</div>;
  10. }
  11. }
  12. <MyComponent foo={42} />;
 

We removed our dependency on prop-types and added a Flow object type named Props with the same shape as the prop types but using Flow’s static type syntax. Then we passed our new Props type into React.Component as a type argument.

Now if you try to use <MyComponent> with a string for foo instead of a number you will get an error.

Now wherever we use this.props in our React component Flow will treat it as the Props type we defined.

Note: If you don’t need to use the Props type again you could also define it inline: extends React.Component<{ foo: number, bar?: string }>.

Note: We import React as a namespace here with import * as React from 'react' instead of as a default with import React from 'react'. When importing React as an ES module you may use either style, but importing as a namespace gives you access to React’s utility types.

React.Component<Props, State> is a generic type that takes two type arguments. Props and state. The second type argument, State, is optional. By default it is undefined so you can see in the example above we did not include State. We will learn more about state in the next section…

Adding State

To add a type for state to your React class component then create a new object type, in the example below we name it State, and pass it as the second type argument to React.Component.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  1. import * as React from 'react';
  2. type Props = { /* ... */ };
  3. type State = {
  4. count: number,
  5. };
  6. class MyComponent extends React.Component<Props, State> {
  7. state = {
  8. count: 0,
  9. };
  10. componentDidMount() {
  11. setInterval(() => {
  12. this.setState(prevState => ({
  13. count: prevState.count + 1,
  14. }));
  15. }, 1000);
  16. }
  17. render() {
  18. return <div>Count: {this.state.count}</div>;
  19. }
  20. }
  21. <MyComponent />;
 

In the example above we are using a React setState() updater function but you could also pass a partial state object to setState().

Note: If you don’t need to use the State type again you could also define it inline: extends React.Component<{}, { count: number }>.

Using Default Props

React supports the notion of defaultProps which you can think of as default function arguments. When you create an element and you did not include a prop with a default then React will substitute that prop with its corresponding value from defaultProps. Flow supports this notion as well. To type default props add a static defaultProps property to your class.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  1. import * as React from 'react';
  2. type Props = {
  3. foo: number, // foo is required.
  4. };
  5. class MyComponent extends React.Component<Props> {
  6. static defaultProps = {
  7. foo: 42, // ...but we have a default prop for foo.
  8. };
  9. }
  10. // So we don't need to include foo.
  11. <MyComponent />
 

Flow will infer the type of your default props from static defaultProps so you don’t have to add any type annotations to use default props.

Note: You don’t need to make foo nullable in your Props type. Flow will make sure that foo is optional if you have a default prop for foo.

Stateless Functional Components

In addition to classes, React also supports stateless functional components. You type these components like you would type a function:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  1. import * as React from 'react';
  2. type Props = {
  3. foo: number,
  4. bar?: string,
  5. };
  6. function MyComponent(props: Props) {
  7. props.doesNotExist; // Error! You did not define a `doesNotExist` prop.
  8. return <div>{props.bar}</div>;
  9. }
  10. <MyComponent foo={42} />
 

Using Default Props for Functional Components

React also supports default props on stateless functional components. Similarly to class components, default props for stateless functional components will work without any extra type annotations.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  1. import * as React from 'react';
  2. type Props = {
  3. foo: number, // foo is required.
  4. };
  5. function MyComponent(props: Props) {}
  6. MyComponent.defaultProps = {
  7. foo: 42, // ...but we have a default prop for foo.
  8. };
  9. // So we don't need to include foo.
  10. <MyComponent />;
 

Note: You don’t need to make foo nullable in your Props type. Flow will make sure that foo is optional if you have a default prop for foo.

type Props={};的更多相关文章

  1. 一步一步带你实现virtual dom(二) -- Props和事件

    很高兴我们可以继续分享编写虚拟DOM的知识.这次我们要讲解的是产品级的内容,其中包括:设置和DOM一致性.以及事件的处理. 使用Babel 在继续之前,我们需要弥补前一篇文章中没有详细讲解的内容.假设 ...

  2. React报错之JSX element type does not have any construct or call signatures

    正文从这开始~ 总览 当我们试图将元素或react组件作为属性传递给另一个组件,但是属性的类型声明错误时,会产生"JSX element type does not have any con ...

  3. React报错之Type '() => JSX.Element[]' is not assignable to type FunctionComponent

    正文从这开始~ 总览 当我们尝试从函数组件中返回元素组成的数组时,会产生"Type '() => JSX.Element[]' is not assignable to type Fu ...

  4. 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)

    前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...

  5. ReactJS入门(三)—— 顶层API

    本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...

  6. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  7. .NET 获取类型中的属性

    解决方案      通过反射的方式获取类型中的所有属性. 引用命名空间 using System.Reflection; 实体类 public class User { private string ...

  8. pjax技术的应用

    一.什么是PJAX? 现在有一些网站(apicloud,  github)支持这样一种浏览方式,当你点击站内的一个连接的时候,不是传统的跳转到另外一个连接,而是类似ajax的局部刷新改变页面内容,但是 ...

  9. 【zepto学习笔记03】事件机制

    前言 我们今天直接进入事件相关的学习,因为近期可能会改到里面的代码就zepto来说,我认为最重要的就是选择器与事件相关了,随着浏览器升级,选择器简单了,而事件相关仍然是核心,今天我们就来学习学习 ze ...

随机推荐

  1. 浏览器缓存机制介绍 + 常用 http 状态码

    浏览器缓存分为两种, 强制缓存  与  协商缓存, https://www.pass4lead.com/300-209.htmlhttps://www.pass4lead.com/300-320.ht ...

  2. 题目1461:Tempter of the bone(深度优先遍历DFS)

    题目链接:http://ac.jobdu.com/problem.php?pid=1461 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. Sencha Touch 实战开发培训 视频教程 第二期 第二节

    2014.4.9晚上8:00分开课. 本节课耗时接近1个半小时,需要一点耐心来观看. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 了解Container: 了解card布 ...

  4. Web Uploader在低版本IE下无法显示Flash的一种情况

    用户反馈在IE 8下无法正常显示Web Uploader控件,并已安装了Flash插件.调试发现在内部抛出了Runtime Error的错误,关键代码如下: Runtime.create = func ...

  5. C语言位操作--两整数中的最大值与最小值

    不用选择分支找出指定两整数中的最大值与最小值: int x; int y; // 找出x与y的最大值与最小值 int r; // r保存结果 r = y ^ ((x ^ y) & -(x &l ...

  6. Docker监控:google/cadvisor

    Docker自带了容器监控功能,可以对容器进行相关的性能监控,指标查看 主要包括: 主机的CPU情况和使用量 主机的内存情况和使用量 主机的本地镜像情况 主机的容器运行情况 常规使用docker ps ...

  7. mysql概要(四)order by ,limit ,group by和聚合函数的特点,子查询

    1.order by 默认按升序排列(asc/desc),多字段排序 order by 字段 排序方式,字段2 排序方式,..: 在分组排序中,排序是对分组后的结果进行排序,而不是在组中进行排序. s ...

  8. Unity3D笔记十四 力

    Unity中力的方式有两种:第一种为普通力,需要设定力的方向与大小:第二种为目标位置力,需要设定目标点的位置,该物体将朝向这个目标位置施加力. 1.案例 添加两个球体和一个立方体,另外还要给两个球体添 ...

  9. React 属性和状态的一些总结

    一.属性 1.第一种使用方法:键值对 <ClaaNameA name = “Tom” /> <ClaaNameA name = {Tom} /> <ClaaNameA n ...

  10. 你不可缺少的技能——Markdown编辑

    Markdown简介 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式.请不要被「标记」.「语言」所迷惑,Markdown 的语法十分 ...