Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separate node module, now we can go one step further and get rid of that extra dependency just by using flow type annotations in our create-react-app project!

Install:

yarn add flow-bin

Scripts:

"flow": "flow"

Run:

npm run flow init
npm run flow

Add flow annotations:

// @flow

import {createStore} from 'redux';
import reducer from './reducers/todo'; export type TodoType = {
id: number,
name: string,
isComplete: boolean
}; export type StateType = {
todos: Array<TodoType>,
currentTodo: string
}; const initState: StateType = {
todos: [
{id: 1, name: 'Render static UI', isComplete: true},
{id: 2, name: 'Create initial state', isComplete: false},
{id: 3, name: 'Render based on state', isComplete: true}
],
currentTodo: 'Temp'
}; const store = createStore(reducer, initState); export default store;

Import a flow type:

// @flow
import React from 'react'
import {connect} from 'react-redux';
import type {TodoType} from '../store'; const TodoItem = ({id, name, isComplete}: TodoType) => (
<li>
<input type="checkbox" defaultChecked={isComplete} />
{name}
</li>
) const TodoList = (props: {todos: Array<TodoType>}) => (
<div className="Todo-List">
<ul>
{props.todos.map(todo => <TodoItem key={todo.id} {...todo} />)}
</ul>
</div>
); export default connect(
(state) => ({todos: state.todos})
)(TodoList);

[React] Remove React PropTypes by using Flow Annotations (in CRA)的更多相关文章

  1. React中的PropTypes详解

    propTypes用来规范props必须满足的类型,如果验证不通过将会有warn提示. React PropTypes的种类有: React.PropTypes.array // 队列 React.P ...

  2. 【react】利用prop-types第三方库对组件的props中的变量进行类型检测

    1.引言--JavaScript就是一个熊孩子   1.1对于JSer们来说,js是自由的,但同时又有许多让人烦恼的地方.javascript很多时候就是这么一个熊孩子,他很多时候并不会像C和java ...

  3. JavaScript 和 React,React用了大量语法糖,让JS编写更方便。

    https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...

  4. React的React Native

    React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...

  5. 重谈react优势——react技术栈回顾

    react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...

  6. react学习-react父组件给子组件传值与设置传值类型以及是否必传参数

    react 父组件给子组件传参时,父组件直接在引入的子组件内写入要传递的参数即可 <HeaderComponent title={"传递的参数"}></Heade ...

  7. 用react-service做状态管理,适用于react、react native

    转载自:https://blog.csdn.net/wo_shi_ma_nong/article/details/100713151 . react-service是一个非常简单的用来在react.r ...

  8. React的React.createRef()/forwardRef()源码解析(三)

    1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...

  9. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

随机推荐

  1. COGS——T 7. 通信线路

    http://www.cogs.pro/cogs/problem/problem.php?pid=7 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 ...

  2. 出乎意料的else语句

    在python中你可能时不时不碰到else语句用在while和for循环中,请不要吃惊,先看看它的作用吧! 实际上在循环语句中,else子句仅仅会在循环完毕后运行.即跳出循环的操作.如break,同一 ...

  3. 解决wget下载文件名乱码的一些方法

    在下载用apache或者nginx做的索引目录时,遇到文件名乱码问题.搜索了不少资料,尝试了好几种方案,大家可以结合使用. 一般情况下加上–restrict-file-names=nocontrol参 ...

  4. 变量get、set设置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Zabbix + Grafana

    Grafana 简介 Grafana自身并不存储数据,数据从其它地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化了图形的展现,可以用来做监控大屏 Grafana ...

  6. web api 特点

    webapi有很多特点(我不想用优点这个词),比如说restful,支持路由,简单,类似mvc controller/action的代码编写方式,灵活的托管方式,和web的集成等等. Web API的 ...

  7. Stacked Autoencoders

    转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...

  8. Statement和ResultSet

    statement.prepareStatement.callableStatement的使用 1.带?参数的使用prepareStatement.这也是使用最多的. 2.不带参数,例如查所用,不需要 ...

  9. Atcoder ABC 071 C,D

    C - Make a Rectangle Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement W ...

  10. idea python notebook连接pyspark

    1.启动pyspark 2.查看pyspark服务的token  jupyter notebook list 查看正在运行的notebook服务以及他们的token 3.在idea里运行noteboo ...