我正在传递对象数组作为有效负载。当它传递到状态时,它将作为单个数组保留。我想逐个传递对象。我该怎么办?
//action
const loadRecord = async => {
const res = await axios.get('/records?_sort=streak&_order=desc')
const records = res.
dispatch({
type: LOAD_RECORD,
payload: res.data
})
}
//reducer
case LOAD_RECORD:
return {
...state,
records: [...state.records, action.payload]
}
我需要将对象传递到记录状态,而不是数组。 TIA
答案 0 :(得分:0)
将payload
数组扩展到records
数组
case LOAD_RECORD:
return {
...state,
records: [...state.records, ...action.payload]
}
答案 1 :(得分:0)
问题出现在减速器内部。 您也必须散布有效载荷。,因为您的有效载荷是一个数组。
//reducer
case LOAD_RECORD:
return {
...state,
records: [...state.records, ...action.payload]
}