简体   繁体   English

这是在Redux中实现中间件的正确方法吗?

[英]Is this the correct way to implement middleware in redux?

I am attempting to debug a redux store for async actions. 我正在尝试调试异步存储的redux存储。 But I am failing to pass dispatch as a function so I will be posting series of questions to help myself find my issue. 但是我无法通过调度功能,所以我将发布一系列问题以帮助自己找到问题。 The first thing I need to ensure is that I am applying redux-thunk properly. 我需要确保的第一件事是我正确地应用了redux-thunk。 So is this the correct way to implement redux middleware? 那么这是实现redux中间件的正确方法吗?

 import { createStore,applyMiddleware,combineReducers,compose } from 'redux'; import thunk from 'redux-thunk'; import {createLogger} from 'redux-logger'; import {inventoryFilter,availableAttributes} from '../reducers/reducer'; const logger=createLogger() const Store = createStore( ///combine imported reducers combineReducers({ activeFilter:inventoryFilter, availableAttributes:availableAttributes },{},applyMiddleware(thunk,logger) )); export default Store; 

No. You're passing the middleware enhancer as an argument to combineReducers , when it should actually be an argument to createStore . 不。您要将中间件增强程序作为参数传递给combineReducers ,而实际上它应该是createStore的参数。

Here's how I would write it: 这是我的写法:

import { createStore,applyMiddleware,combineReducers,compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import {createLogger} from 'redux-logger';
import {inventoryFilter,availableAttributes} from '../reducers/reducer';


const rootReducer = combineReducers({
    activeFilter:inventoryFilter,
    availableAttributes:availableAttributes
});

const loggerMiddleware = createLogger();
const middlewareEnhancer = applyMiddleware(thunkMiddleware, loggerMiddleware);

const store = createStore(rootReducer, middlewareEnhancer);

export default store;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM