Hibop / Hibop.github.io

Hibop 个人博客
https://hibop.github.io/
23 stars 1 forks source link

关于函数式编程——优美的redux码源 #28

Open Hibop opened 6 years ago

Hibop commented 6 years ago

梳理下redux Api

react-redux

const mapDispatchToProps = ( dispatch, ownProps ) => { return { onClick: () => { dispatch({ type: 'SET_VISIBILITY_FILTER', filter: ownProps.filter }); } }; }


## createStore内部的发布订阅原理
```js
const createStore = (reducer) => {
  let state;
  let listeners = [];

  const getState = () => state;

  const dispatch = (action) => {
    state = reducer(state, action);
    listeners.forEach(listener => listener());
  };

  const subscribe = (listener) => {
    listeners.push(listener);
    return () => {
      listeners = listeners.filter(l => l !== listener);
    }
  };

  dispatch({});

  return { getState, dispatch, subscribe };
};

《JavaScript 轻量级函数式编程》|《你不知道的JS》姊妹篇

https://segmentfault.com/a/1190000012394641

Hibop commented 6 years ago

Redux FAQ