我试图在我的reactjs应用中应用redux。由于这些错误,我无法继续:
我确定我已经安装了所需的所有依赖项。这是我的package.json
的相关部分"dependencies": {
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-promise": "^0.5.3",
"redux-thunk": "^2.2.0",
}
这是我的index.js的一部分,它实现了redux
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import reducers from './reducers';
const logger = createLogger();
const store = createStore(reducers,
applyMiddleware(
thunkMiddleware, logger
)
)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
答案 0 :(得分:24)
根据docs,您混淆了redux-logger
您需要导入特定的createLogger
功能
import { createLogger } from 'redux-logger'
const logger = createLogger({
// ...options
});
或使用默认导入
import logger from 'redux-logger'
然后你的代码应该没问题
const store = createStore(
reducers,
applyMiddleware(thunkMiddleware, logger)
)
答案 1 :(得分:1)
我没有在您的依赖项中看到react和react-dom,并且它不包含在import语句中。
让我为您提供一个最近我如何使用应用程序的示例,同样使用了令人惊叹的redux开发人员工具。
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import reduxThunk from 'redux-thunk';
import App from './components/App';
import authReducer from '../reducers/auth';
import urlsReducer from '../reducers/urls';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
//Store creation
const store = createStore(
combineReducers({
auth: authReducer,
urls: urlsReducer
}),
composeEnhancers(applyMiddleware(reduxThunk))
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
我认为你只需要在reduxThunk之后添加logger。如果你已经合并了减速器,那么只需提供减速器而不是combineReducers。
此致 圣拉斐尔
答案 2 :(得分:0)
我遇到相同的错误 TypeError:中间件不是函数。
import { createStore, combineReducers, applyMiddleware } from "redux";
import { createLogger } from "redux-logger";
import thunk from "redux-thunk";
import math from "./reducers/mathReducer";
import user from "./reducers/userReducer";
const logger = createLogger();
export default createStore(
combineReducers({ math, user }),
{},
applyMiddleware(logger, thunk)
);
用applyMiddleware(logger, thunk)
代替applyMiddleware(logger, thunk.default)
对我有用。
答案 3 :(得分:0)
最简单的解决方法
import * as thunk from 'redux-thunk';
createStore(rootReducer, InitialState, applyMiddleware(thunk.default));
答案 4 :(得分:0)
我试图仅在开发中将logger
添加到中间件列表中,但弄乱了导入并收到了相同的错误。要解决我的问题,我需要在.default
上添加require()
:
const middleware = []
if (process.env.NODE_ENV === 'development') {
middleware.push(require('redux-logger').default)
}
答案 5 :(得分:0)
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import reduxPromiseMiddleware from 'redux-promise-middleware';
import { applyMiddleware, createStore } from 'redux';
const middleware = applyMiddleware(reduxPromiseMiddleware, thunk, logger);
const store = createStore(reducer, middleware);
答案 6 :(得分:0)
请找到以下解决方案代码:
const thunkMiddleware = require('redux-thunk').default;
const store = createStore(reducer, applyMiddleware(thunkMiddleware));
答案 7 :(得分:0)
就我而言,我在 store 方法之前调用了中间件函数