gpt4 book ai didi

javascript - React Reducer 返回带状态数组的正确方法

转载 作者:行者123 更新时间:2023-11-30 14:24:09 26 4
gpt4 key购买 nike

我在 action creator 和 redux-promise 中间件中使用 axios 将对象数组返回到组件渲染方法中的 map 函数。当使用带有 ES6 spread 的第一个 return 语句时,我得到了一个数组中的数组。我将如何正确地遍历这个数组?我会在 reducer 中使用 map 吗?第二个返回语句有效。我不清楚为什么我不想自己返回数组。谢谢!!

const INITIAL_STATE = [];

export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case GET_TICKET:
return [action.payload.data, ...state];
return action.payload.data;
default:
return state;
}
}

最佳答案

将两个数组合二为一的方法主要有两种:

const newArray = [...firstArray, ...secondArray];
// or
const newArray = firstArray.concat(secondArray);

这些都适用于您的情况,例如:

return state.concat(action.payload.data);

关于javascript - React Reducer 返回带状态数组的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52208156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com