[英]Modifying state & the correct way to update my reducer? (React / Redux)
[英]React Reducer Correct Way To Return An Array With State
我在動作創建者和redux-promise中間件中使用axios將對象數組返回到組件的render方法中的map函數。 在ES6擴展中使用第一個return語句時,我在數組中得到一個數組。 我如何正確遍歷此數組? 我會在化簡器中使用map嗎? 第二個return語句起作用。 我不清楚為什么我不想只返回數組。 謝謝!!
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 INITIAL_STATE = { data : [] };
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case GET_TICKET:
return {...state, data: action.payload.data};
default:
return state;
}
}
有兩種將兩個數組合並為一個數組的主要方法:
const newArray = [...firstArray, ...secondArray];
// or
const newArray = firstArray.concat(secondArray);
這些都將適合您的情況,例如:
return state.concat(action.payload.data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.