代码之家  ›  专栏  ›  技术社区  ›  noone000

redux thunk,将其用作列表或对象的区别

  •  1
  • noone000  · 技术社区  · 6 年前

    我正在阅读我在Github上找到的一个React项目的代码,我发现Redux Thunk的另一种用法,通过查看Github上的官方文档,他们使用它的方式是:

    import { createStore, applyMiddleware } from 'redux';
    import thunk from 'redux-thunk';
    import rootReducer from './reducers/index';
    
    // Note: this API requires redux@>=3.1.0
    const store = createStore(
      rootReducer,
      applyMiddleware(thunk)
    );
    

    但在代码里我发现他用了:

    import { AsyncStorage } from 'react-native';
    import { applyMiddleware, createStore } from 'redux';
    import { autoRehydrate, persistStore } from 'redux-persist'
    import thunk from 'redux-thunk';
    import reducers from '../reducers';
    
    const middleWare = [thunk];
    
    const createStoreWithMiddleware = applyMiddleware(...middleWare)(createStore);
    
    export default configureStore = (onComplete) => {
      const store = autoRehydrate()(createStoreWithMiddleware)(reducers);
      persistStore(store, { storage: AsyncStorage }, onComplete);
    
      return store;
    };
    

    将它用作列表或对象有什么区别?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Matei Radu Ciaran George    6 年前

    让我们看看函数签名( source ):

    function applyMiddleware(...middlewares) { /* logic */ }
    

    applyMiddleware 使用 rest parameter syntax 它允许表示不定数量的参数 作为数组 .

    因为这个,两个

    applyMiddleware(thunk, otherMiddleware);
    

    const middlewares = [thunk, otherMiddleware];
    applyMiddleware(...middlewares);
    

    同样有效。