[React] Remove React PropTypes by using Flow Annotations (in CRA)
2017-09-02 16:50
549 查看
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:
Scripts:
Run:
Add flow annotations:
Import a flow type:
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] Define defaultProps and PropTypes as static methods in class component
- 解决警告:View.propTypes has been deprecated and will be removed in a future version of ReactNative
- webpack打包遇到locals[0] does not appear to be a `module` object with Hot Module replacement API enabled. You should disable react-transform-hmr in production by using `env` section in Babel configuration
- 个人收藏 Dropdown by index in ALV using in Web Dynpro
- add more columns in join table using spring, hibernate, JPA Annotations
- Serialize and deserialize objects as Xml using generic types in C# 2.0
- Installing and Using Standby Statspack in 11g
- React PropTypes
- [TypeScript] Using Assertion to Convert Types in TypeScript
- How to use insert or retrieve data by using Core Data in iOS
- Liferay get Organization by using User or get All Organizations in liferay database
- [React] Optimistic UI update in React using setState()
- Using JPA and JAXB Annotations in the Same Object
- react PropTypes
- how to write your annotation types and make use of built-in annotations to control their behavior
- [ReactVR] Add Shapes Using 3D Primitives in React VR
- how to select data in multiple datatables after using join,group by and order by in Linq
- react-native-baidu-map undefined is not an object(evaluting 'a.PropTypes.bool')
- react开发报错Use the prop-types package from npm instead.
- React总结7:Each child in an array or iterator should have a unique "key" prop.的处理方法