我已经上了一些关于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



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

post man response attached
发布评论
评论(1)
看起来您的还原逻辑是错误的:
根据屏幕截图,您的
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:
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 indeedundefined
, 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