我试图在用数据填充后从 reducer 返回一个数组,但它总是返回一个空数组。
export default function dashboards(state: any = dashboardsState, action: any) {
// more code
if (action.type === Types.LOAD_DASHBOARD_SUCCESS) {
// create the cards holder in the store
var cardsSkull: any = [];
Object.keys(action.payload.lists).map((idLis: any) => {
cardsSkull[idLis] = {};
console.log("still up");
});
action.payload.importedDashboards.map((impDash: any) => {
Object.keys(impDash.mappedLists).map((key: any) => {
const mpList = impDash.mappedLists[key];
cardsSkull[mpList.idlistLocal][impDash.remote_board_id] = [1, 2, 3];
console.log("still working");
});
});
console.log(cardsSkull);
// assign and ready to go
return Object.assign({}, state, {
selectedDashboardData: action.payload,
cards: cardsSkull
});
}
// more code
return state;
}
当我 console.log(cardsSkull)
时,一切看起来都正确。
我希望 cards
在填充后具有 cardsSkull
的值,但实际输出是一个空数组
最佳答案
您将 cardsSkull
定义为数组(底层是一个对象),但是因为 idLis
不是数字,cardsSkull[idLis] = {} ;
不填充对象数组部分中的元素,而是填充数组对象本身中的元素。 因此在你的屏幕截图中,你的数组的 length
属性仍然是 0
! 使用数字作为 idLis
和你的问题解决了。
关于javascript - Reducer 不断返回一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55771000/