0

I had an action make an api call to a backend to retrive an array of objects. The action then dispatches an update to a state variable. At this point I meet the following error:

Objects are not valid as a React child (found: object with keys {x, y}). 
If you meant to render a collection of children, use an array instead.

The following code has been minimized to still produce the error:

getDonationData : (reportType, recordCount) => (dispatch => {
    return Axios.post(`http://localhost:4000/reports/`,
      {
      reportType, recordCount
    }
    )
      .then(apiResponse => {
        const returnedArray = apiResponse.data;
        dispatch({
          type : 'reportData',
          payload : [{x: 1, y: 2}, {x: 4, y: 3}]
        })
      })
  })

If the objects in the array are replaced by numbers like [1, 2, 3] the error goes away.

Does anyone know why this is happening and how to work around the possible nesting limitation? My actual data has about 500 items and the x values are also date objects (this is chart data).

Reducer code:

case 'reportData':
      return {
        ...previousState,
        reportData : action.payload
      };

1 Answer 1

1

This error is might from component where you are accessing reportData, may be you are trying to display object (ex. {x:1, y:2}) from reportData instead of single data (ex. x).

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct, it was in the prop passdown that the error was being raised: reportData={this.props.reportData}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.