gpt4 book ai didi

reactjs - 如何向Redux添加多个中间件?

转载 作者:行者123 更新时间:2023-12-03 12:54:47 25 4
gpt4 key购买 nike

我已经插入了一个中间件,redux-thunk,我想添加另一个中间件,redux-logger

如何配置它以便我的应用程序使用这两个中间件?我尝试传递 [ReduxThunk, logger] 数组,但这不起作用。

代码:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import logger from 'redux-logger';

import App from './components/app';
import reducers from './reducers';
require('./style.scss');

const createStoreWithMiddleware = applyMiddleware(ReduxThunk)(createStore);

ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>,
document.querySelector('#app')
);

最佳答案

applyMiddleware将每个中间件作为一个新参数(而不是数组)。因此,只需传入您想要的每个中间件即可。

const createStoreWithMiddleware = applyMiddleware(ReduxThunk, logger)(createStore);

关于reactjs - 如何向Redux添加多个中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com