React Redux with multi applyMiddleware

时间:2017-09-26 07:41:38

标签: javascript reactjs react-native redux redux-thunk

我想使用以下答案 Redux state persistence with a database

import {createStore, compose, applyMiddleware} from 'redux';

const localStorageMiddleware = ({getState}) => {
  return (next) => (action) => {
    const result = next(action);
    localStorage.setItem('applicationState', JSON.stringify(
      getState()
    ));
    return result;
  };
};

const store = compose(
  applyMiddleware(
    localStorageMiddleware
  )
)(createStore)(
  reducer,
  JSON.parse(localStorage.getItem('applicationState'))
)

但我不明白这里的javascript语法 他如何使用compose(argument)(createStore)(argument)

是否有像createStore(reducer,initialState,compose(argument))这样的替代方案?

此外,如何在此处传递初始状态

const createStoreWithMiddleware = compose(  
  applyMiddleware(thunkMiddleware)
)(createStore);

export default function configureStore(initialState) {  
  const store = createStoreWithMiddleware(rootReducer);
  return store;
}

1 个答案:

答案 0 :(得分:1)

是的! enhancer()(createStore)语法是使用createStore的旧式方式,我们试图鼓励人们使用更新的语法:

const composedEnhancers = compose(
    firstEnhancer,
    secondEnhancer
);

const store = createStore(
    rootReducer,
    preloadedState,
    composedEnhancers
);