Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recompose withReducer #18

Open
monsterooo opened this issue Apr 12, 2018 · 0 comments
Open

recompose withReducer #18

monsterooo opened this issue Apr 12, 2018 · 0 comments

Comments

@monsterooo
Copy link
Owner

monsterooo commented Apr 12, 2018

withReducer 介绍

withReducer类似于withState(),但是它的更新模式使用了redux相通的方法。withReducer接收4个参数,第一个参数为state 名称,第二个参数为dispatch 方法名,第三个参数为state 更新函数,第四个参数为初始化状态。withReducerwithState是同样的操作,他们的数据都是保存在代理组件的state中。再向外暴露一个更新的方法。

withReducer Flow Type

withReducer<S, A>(
  stateName: string,
  dispatchName: string,
  reducer: (state: S, action: A) => S,
  initialState: S | (ownerProps: Object) => S
): HigherOrderComponent

withReducer 实例

const { compose, withReducer } = Recompose;

const Foo = compose(
  withReducer('state', 'dispatch', (state, action) => {
    if(action.type === 'SETCOUNTER') {
      return { ...state, counter: action.value };
    }
    return state;
  }, { counter: 0 }),
)(({ state: { counter }, dispatch }) => (
  <div>
    <p>Counter: {counter}</p>
    <button onClick={() => dispatch({ type: 'SETCOUNTER', value: 10 })}>设置Counter</button>
  </div>
))

在线DEMO

codepen在线预览

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant