分别调度对象数组

时间:2019-07-16 10:43:16

标签: reactjs

我正在传递对象数组作为有效负载。当它传递到状态时,它将作为单个数组保留。我想逐个传递对象。我该怎么办?

//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]
      }

I'm currently getting this.

我需要将对象传递到记录状态,而不是数组。 TIA

2 个答案:

答案 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]
      }