react-props
inject props to react component for high performance rendering
demo
使用方法
第一步:使用 injectProps
const ENTER_KEY = 13const ESCAPE_KEY = 27 @ static propTypes = addItem: PropTypesfuncisRequired { this } { let keyCode = ekeyCode if keyCode === ENTER_KEY || keyCode === ESCAPE_KEY this } { let title = inputvalue if title thisprops inputvalue = '' } { return <header id="header"> <h1>todos</h1> <input id="new-todo" placeholder="What needs to be done?" onBlur= thishandleBlur onKeyUp= thishandleKeyup /> </header> }
第二步:编写 selector
selector 的作用是从 global state 中取出一份数据,合并到 react 组件的 props 属性。
// selectors.js let { return actions} let { let todos = state return ...actions isAllCompleted: !!todoslength && todos } let { let todos activeFilter = state return ...actions todos: todos }
selector 函数前三个参数为固定的。
- state: 全局 state 数据
- actions: flux 的 actions 函数集
- props: 父组件传递过来的初始 props
第三步,去掉相关父组件手动传 props 的做法
@ { return <div> <NewTodo /> // 裸组件 <Main id="main" /> // 或者只传关键属性 <Filters /> </div> }
第四步,编写 match 函数
match 函数响应 action 调用,返回需要更新的 selector 名
let { let key = data }
第五步,设置 flux 配置
flux 配置让 react-props 能拿到必要的数据
let store = let getState actions = store
最后一步,侦听 action
如果你用 redux,则如下配置:
reducer = let store =
如果你用 refer,则如下配置:
let store = let getState actions = store
如果你想手动处理,则:
let data = type: 'ADD_TODO' text: 'text for todo' // match 函数将拿到这个 data
此外,不需要再做什么,视图会根据 match 函数的返回值做局部更新(当更新范围为 Root 组件时,也就相当于全局更新).