代码之家  ›  专栏  ›  技术社区  ›  Soroush Chehresa TommyF

如何将参数传递到Redux中间件?

  •  0
  • Soroush Chehresa TommyF  · 技术社区  · 7 年前

    我想把一个参数传递到一个定制的Redux中间件中。 但是我不知道如何在自定义中间件中使用参数。

    例子:

    import { createStore, applyMiddleware } from 'redux';
    import reducers from './reducers';
    
    const customMiddleWare = store => next => action => {
      // how to use argument here?
    };
    
    const middlewares = [customMiddleWare(argument)];
    
    const store = createStore(
      reducers,
      applyMiddleware(...middlewares)
    );
    
    0 回复  |  直到 7 年前
        1
  •  3
  •   Soroush Chehresa TommyF    7 年前

    由于中间件是函数,您可以创建一个包装器函数,该函数将接受您的参数并返回您的中间件函数,该函数将访问所提供的参数。

    例子:

    const middlewareWrapper = customArgument => store => next => action =>
      console.log(customArgument);
    }
    
    const middlewares = [middlewareWrapper(argument)];
    
    const store = createStore(
      reducers,
      applyMiddleware(...middlewares)
    );
    
    推荐文章