React Redux未显示正确的数据

发布于 2025-02-06 17:35:24 字数 1918 浏览 3 评论 0 原文

我已经上了一些关于React和Redux的课程,到目前为止,挑战仍在堆积。我正在创建一个简单的应用程序,当时我需要仅映射API的响应,但是状态看起来很奇怪。这些动作正在返回数据,但显示出尴尬。请查看所附图像。有人知道为什么数据的console.log返回一个数字而不是对象或数组名称吗?另外,如果您从Redux Dev工具中看到状态,则可以看到在显示对象名称的位置,而是看到未定义的。请帮忙。我已经呆了几天了,一直把我的头撞在墙上。 enter Image描述在这里

reducer
import _ from 'lodash';

import { 
FETCH_CLIENT,
FETCH_CLIENTS,
ADD_CLIENTS_COMMENTS
} from "../actions/types";

export default (state = {}, action) => {
switch (action.type) {
case FETCH_CLIENT:
  return { ...state, ..._.mapKeys(action.payload, 'ClientUno') };
case FETCH_CLIENTS:
  return { ...state, [action.payload.ClientUno]: action.payload };
case ADD_CLIENTS_COMMENTS:
    return { ...state, [action.payload.ClientUno]: action.payload }; 
default:
  return state;
}
 };
 
combined reducer
import { combineReducers } from "redux";
import ClientsReducer from "./ClientsReducer";

export default combineReducers({
clients:ClientsReducer
});

action
const mapStateToProps = state => {
return{
    clients:Object.values(state.clients),
}
}

export default connect(mapStateToProps,{fetchAllClients})(Clients);

删除.clientuno后。这是我的新状态

“在此处输入图像描述”

post Man响应 ”在此处输入图像说明”

i've taken a few courses on react and redux and so far the challenges are still piling up. i'm creating a simple app were at the time i need to just map the response from the api, but the state looks weird. the actions is returning the data, but is being displayed awkward. Please see the images attached. Does anyone know why the console.log of the data returns a number instead of an object or array name? also if you see the state from the redux dev tools, you can see that where an object name would be displayed, you see undefined instead. Please help. I've been at it for a couple of days and have been banging my head against the wall. enter image description here

enter image description here
enter image description here
enter image description here

reducer
import _ from 'lodash';

import { 
FETCH_CLIENT,
FETCH_CLIENTS,
ADD_CLIENTS_COMMENTS
} from "../actions/types";

export default (state = {}, action) => {
switch (action.type) {
case FETCH_CLIENT:
  return { ...state, ..._.mapKeys(action.payload, 'ClientUno') };
case FETCH_CLIENTS:
  return { ...state, [action.payload.ClientUno]: action.payload };
case ADD_CLIENTS_COMMENTS:
    return { ...state, [action.payload.ClientUno]: action.payload }; 
default:
  return state;
}
 };
 
combined reducer
import { combineReducers } from "redux";
import ClientsReducer from "./ClientsReducer";

export default combineReducers({
clients:ClientsReducer
});

action
const mapStateToProps = state => {
return{
    clients:Object.values(state.clients),
}
}

export default connect(mapStateToProps,{fetchAllClients})(Clients);

after removing .ClientUno. This my new state

enter image description here

post man response attachedenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2025-02-13 17:35:24

看起来您的还原逻辑是错误的:

return { ...state, [action.payload.ClientUno]: action.payload }

根据屏幕截图,您的 action.payload 是项目数组。单个项目确实具有 .clientuno 字段。但是 array 肯定不会。因此, action.payload.clientuno 确实是 undefined ,这就是您用作密钥的内容。

您需要重新考虑要用作钥匙的内容。我不知道您实际上是如何构建此切片状态的,因此我无法在那里提供建议。

i Will 请注意,您正在使用的REDUX模式非常过时。带有Redux工具包的“ Modern Redux”更简单,更易于使用 - 没有“ Action_Types” ,Switch语句或对象差。请参阅我们的官方Redux Docs教程,以学习如何使用Redux Toolkit:

https:// https:/ /教程/索引

It looks like your reducer logic is wrong:

return { ...state, [action.payload.ClientUno]: action.payload }

Per the screenshots, your action.payload is an array of items. The individual items do have a .ClientUno field inside them. But the array most definitely will not. So, action.payload.ClientUno is indeed undefined, and that's what you're using as a key.

You'll need to rethink what you want to use as a key there. I don't know how you're actually trying to structure this slice state, so I can't provide a suggestion there.

I will note that the Redux patterns you're using here are very outdated. "Modern Redux" with Redux Toolkit is much simpler and easier to use - there's no "ACTION_TYPES", switch statements, or object spreads. Please see our official Redux docs tutorials to learn how to use Redux Toolkit:

https://redux.js.org/tutorials/index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文