plugin/extension

plugin:对本来系统原有的进行修饰decorate/补充
extension:对系统没有的进行添加increase

Redux模型

antion 描述发生了什么

  1. export const add = () => {
  2. return {
  3. type: ADD
  4. }
  5. }

reducers:描述了action如何改变state.根据action 更新 state,接收旧的 state 和 action,返回新的 state
在default 情况下返回旧的state遇到未知的 action 时,一定要返回旧的state

  1. const INITIAL_STATE = {
  2. num: 0
  3. }
  4. export default function counter (state = INITIAL_STATE, action) {
  5. switch (action.type) {
  6. case ADD:
  7. return {
  8. ...state,
  9. num: state.num + 1
  10. }
  11. default:
  12. return state
  13. }
  14. }

store[唯一]:联系reducers和action. state 被储存在一棵 object tree 中,并且这个 object tree 只存在于唯一一个 store

  1. const INIT_STATE = {
  2. image: '',
  3. icon: '',
  4. text: '',
  5. status: '',
  6. duration: 3000,
  7. hasMask: false,
  8. isOpened: false
  9. }
  10. const composeEnhancers =
  11. typeof window === 'object' &&
  12. window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
  13. window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
  14. // Specify extensions options like name, actionsBlacklist, actionsCreators, serialize...
  15. }) : compose
  16. const middlewares = [
  17. thunkMiddleware
  18. ]
  19. if (process.env.NODE_ENV === 'development') {
  20. middlewares.push(require('redux-logger').createLogger())
  21. }
  22. const enhancer = composeEnhancers(
  23. applyMiddleware(...middlewares),
  24. )
  25. export default function configStore () {
  26. const store = createStore(rootReducer, enhancer)
  27. return store;
  28. }