【发布时间】:2021-11-03 09:02:38
【问题描述】:
我正在尝试使用redux state management 和react 制作应用程序。我需要来自 API 的fetch,并且我已将所有 API 逻辑放在另一个文件中,我试图将来自state 的响应中的数据放在redux 中。我试过了:
API 脚本文件:
import store from "./store";
import foo from "./foo.js";
fetch("http://my_fantastic_api.com/fooBar/");
.then((response)=>{
if (reponse.status.OK){
//The error:
store.dispatch({
type:"MODIFY_XYZ"
});
}
}).catch(error =>{
throw new error()
})
但是当我尝试此方法时,当按下按钮调用fetch 函数时遇到此错误,发生错误。
错误信息:
Unhandled Rejection (Error): When called with an action of type "MODIFY_XYZ", the slice reducer for key "FOO_BAR" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.
如果有人有任何想法或建议我非常感谢他们,我正在尝试找到解决此错误的方法。
向阿尔文问好。
编辑:
存储文件:
//Importing packages
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App.jsx";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "react-redux";
import { createStore } from "redux";
import reducers from "./redux/reducers/index.js";
//Redux configuration
export const store = createStore(reducers);
//Render app
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
【问题讨论】:
-
可以分享一下店铺文件代码吗?
-
我已经更新了帖子并包含了商店文件
标签: javascript reactjs redux react-redux frontend