我的错误消息:
"错误:操作必须是纯对象。实际类型为:"undefined"。您可能需要在商店设置中添加中间件来处理调度其他值,例如"redux thunk"来处理调度功能。看见https://redux.js.org/tutorials/fundamentals/part-4-store#middleware和https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-以redux-thunk中间件为例">
我找到了很多关于这个错误的答案,但没有一个对我有帮助
我在我的react应用程序中存储了一些部件。最后一部分,提出错误。
我真的很困惑,按照所有的答案,我应该如何创建商店。
我的index.js文件:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import '../node_modules/video-react/dist/video-react.css'; // import css
import thunk from 'redux-thunk';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { combineReducers,compose ,applyMiddleware,createStore } from 'redux';
import { file, user,school,student } from "./Utilities/store/reducers";
const combine = combineReducers(
{
filePart: file,
userPart: user,
schoolPart:school,
student:student
}
);
ReactDOM.render(<BrowserRouter>
<Provider store={createStore(combine, applyMiddleware(thunk))}>
<App />
</Provider>
</BrowserRouter>, document.getElementById('root'));
student.js减速器:
import {type} from './../functions/student'
import * as functions from './../functions/student'
const initilize = {
all: [],
schools:[],
courses:[]
};
export const student = (state = initilize, action) => {
switch (action.type) {
case type.get: return functions.getCurrent(state);
case type.fill: return functions.fill(state,action.payload);
}
return state;
}
student.js操作:
import * as functions from './../functions/student'
import { type } from './../functions/student'
export const getCurrent = () => {
return { type: type.get };
}
export const fill = (post) => {
return { type: type.fill, payload: post }
}
export const get = (students, teacherId) => {
if (students && students.all.length > 0) {
getCurrent();
}
if (students === undefined || students.all.length === 0) {
return async(dispatch) => {
let result = await functions.get(teacherId);
dispatch(fill(result));
}
}
else
getCurrent();
}
当我调用actionget((时会发生错误
如果(students === undefined || students.all.length === 0)
不是true
,则该方法不会返回任何内容。但你在某个地方dispatch(get(students, teacherId))
,所以本质上你是dispatch(undefined)
让它总是返回一些东西:
export const get = (students, teacherId) => {
return async(dispatch) => {
if (students && students.all.length > 0) {
getCurrent();
}
if (students === undefined || students.all.length === 0) {
let result = await functions.get(teacherId);
dispatch(fill(result));
}
}
}
}