- 40
- 0
原来这么用是没有问题的,自从更新了create-react-app重新eject与更新了一些依赖库就出现了问题
1. store
export const history = createHashHistory({
basename: '',
hashType: 'slash',
});
const initialState = {};
const enhancers = [];
const middleware = [
thunk,
routerMiddleware(history),
];
if (process.env.NODE_ENV === 'development') {
const devToolsExtension = window.devToolsExtension;
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension());
}
}
const composedEnhancers = compose(
applyMiddleware(...middleware),
...enhancers,
);
const store = createStore(
rootReducer,
initialState,
composedEnhancers,
);
export default store;
2. action
export const FETCH_EXPORT_STAGE = 'FETCH_EXPORT_STAGE'
const makeActionCreator = (type, ...argNames) => (...args) => {
const action = { type };
argNames.forEach((arg, index) => {
action[argNames[index]] = args[index];
});
return action;
};
export const pushExportStage = makeActionCreator(FETCH_EXPORT_STAGE, 'payload')
export const fetchExportStage = () => async (dispatch) => {
const stages = await fetch('/api/findSchoolType')
dispatch(pushExportStage(stages))
return stages
}
3.component
componentDidMount () {
this.props.fetchExportStage()
.then(stages => {
if (!stages.length) return
const defaultStageId = stages[0].id
this.props.fetchExportSchoolByStage(defaultStageId).then(schools => {
this.setState({schools})
})
this.props.fetchExportIndicatorByStage(defaultStageId).then(indicators => {
this.setState({indicators})
})
});
}
报错信息
×
Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions.
▶ 6 stack frames were collapsed.
(anonymous function)
src/routes/home/components/index.js:48
45 | .then(stages => {
46 | if (!stages.length) return
47 | const defaultStageId = stages[0].id
> 48 | this.props.fetchExportSchoolByStage(defaultStageId).then(schools => {
49 | ^ this.setState({schools})
50 | })
51 | this.props.fetchExportIndicatorByStage(defaultStageId).then(indicators => {
package.json
"react": "^16.5.2",
"react-redux": "^5.0.7",
"react-redux-loading-bar": "^4.0.8",
"react-router-dom": "^4.3.1",
"react-router-redux": "^5.0.0-alpha.9",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0", - 共 0 条
- 全部回答
-
残あ梦ぎ鱼 普通会员 1楼
在Redux中,当你尝试在一个异步action creator中调用另一个异步action时,如果遇到“缺少中间件”的错误提示,这通常意味着你没有正确地应用Redux Thunk或其他支持处理异步逻辑的中间件。
Redux Thunk是一个Redux中间件,它允许你的action creator返回一个函数而不是一个动作对象。这个返回的函数可以延迟dispatch动作,甚至 dispatch多个动作,这对于处理复杂的异步逻辑非常有用。
例如:
```javascript import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from './reducers';
const store = createStore( rootReducer, applyMiddleware(thunk) // 这里是添加Redux Thunk中间件的关键步骤 );
// 然后你可以定义异步action creator function fetchUser(id) { return function (dispatch) { dispatch({ type: 'FETCH_USER_REQUEST' });
return fetch(`/api/users/${id}`).then(response => response.json().then(user => { dispatch({ type: 'FETCH_USER_SUCCESS', user }); }) ).catch(error => { dispatch({ type: 'FETCH_USER_FAILURE', error }); });}; }
// 在另一个异步action creator中调用它 function doSomethingAsync() { return function (dispatch, getState) { dispatch(fetchUser(123)); // 调用异步action // ... } } ```
如果你在尝试上述操作时遇到“缺少中间件”的错误,检查你的store配置,确保已正确安装并应用了Redux Thunk中间件。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

