type Props={};
Components
Learn how to type React class components and stateless functional components with Flow
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 |
|
Now, let’s Flowify the component we just wrote:
1 |
|
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
Propstype again you could also define it inline:extends React.Component<{ foo: number, bar?: string }>.
Note: We import
Reactas a namespace here withimport * as React from 'react'instead of as a default withimport 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 |
|
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
Statetype 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 |
|
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
foonullable in yourPropstype. Flow will make sure thatfoois optional if you have a default prop forfoo.
Stateless Functional Components
In addition to classes, React also supports stateless functional components. You type these components like you would type a function:
1 |
|
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 |
|
Note: You don’t need to make
foonullable in yourPropstype. Flow will make sure thatfoois optional if you have a default prop forfoo.
type Props={};的更多相关文章
- 一步一步带你实现virtual dom(二) -- Props和事件
很高兴我们可以继续分享编写虚拟DOM的知识.这次我们要讲解的是产品级的内容,其中包括:设置和DOM一致性.以及事件的处理. 使用Babel 在继续之前,我们需要弥补前一篇文章中没有详细讲解的内容.假设 ...
- React报错之JSX element type does not have any construct or call signatures
正文从这开始~ 总览 当我们试图将元素或react组件作为属性传递给另一个组件,但是属性的类型声明错误时,会产生"JSX element type does not have any con ...
- React报错之Type '() => JSX.Element[]' is not assignable to type FunctionComponent
正文从这开始~ 总览 当我们尝试从函数组件中返回元素组成的数组时,会产生"Type '() => JSX.Element[]' is not assignable to type Fu ...
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...
- ReactJS入门(三)—— 顶层API
本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- .NET 获取类型中的属性
解决方案 通过反射的方式获取类型中的所有属性. 引用命名空间 using System.Reflection; 实体类 public class User { private string ...
- pjax技术的应用
一.什么是PJAX? 现在有一些网站(apicloud, github)支持这样一种浏览方式,当你点击站内的一个连接的时候,不是传统的跳转到另外一个连接,而是类似ajax的局部刷新改变页面内容,但是 ...
- 【zepto学习笔记03】事件机制
前言 我们今天直接进入事件相关的学习,因为近期可能会改到里面的代码就zepto来说,我认为最重要的就是选择器与事件相关了,随着浏览器升级,选择器简单了,而事件相关仍然是核心,今天我们就来学习学习 ze ...
随机推荐
- MyBatis学习之输入输出类型
1. 传递pojo对象 Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称,其中,#{}:占位符号,好处防止sql注入,${}:sql拼接符号, 简要说明 ...
- Android开发之ActionBar
使用微信APP的小伙伴对于微信的ActionBar一定有印象,今天就带领大家一起实现以下这个效果. 第一步打开我们的开发工具,这里我使用的是Eclipse+ADT插件,然后创建我们的工程,这里选择An ...
- facebook login issue
If enable the facebook account in settings, when change account can't open the session. -(void)fbRes ...
- 【CF875F】Royal Questions 最小生成基环树森林
[CF875F]Royal Questions 题意:国王的n个王子该结婚了!现在从外国来了m位公主,第i位公主的嫁妆是wi.由于进步思想的传播,每个公主在选择配偶的事情上是有自主权的,具体地,每个公 ...
- Spark2 Dataset统计指标:mean均值,variance方差,stddev标准差,corr(Pearson相关系数),skewness偏度,kurtosis峰度
val df4=spark.sql("SELECT mean(age),variance(age),stddev(age),corr(age,yearsmarried),skewness(a ...
- vue之用法
一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...
- php curl常见错误:SSL错误、bool(false)
症状:php curl调用https出错 排查方法:在命令行中使用curl调用试试. 原因:服务器所在机房无法验证SSL证书. 解决办法:跳过SSL证书检查. curl_setopt($ch, CUR ...
- .Net Identity OAuth 2.0 SecurityStamp 使用
起源: 近期帮别人做项目,涉及到OAuth认证,服务端主动使token失效,要使对应用户不能再继续访问,只能重新登陆,或者重新授权. 场景: 这种场景OAuth2.0是支持的,比如用户修改了密码,那所 ...
- iOS - UITableView 编辑(cell的插入, 删除, 移动)
UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelega ...
- arcgis api for JavaScript _加载三维图层(scene layer)
arcgis api for JavaScript _加载三维图层(scene layer) arcgis api for JavaScript 4.x 版本增加对三维的支持. 关于三维图层(sce ...