Can I send an array of objects to child component in React

Can I send an array of objects to child component in React

I am sending data to a child component that is an array of objects:

But when I try to render anything with that data, I keep running into errors.

First, I tried to render the first element of the array in a <p></p> tag just to see what was happening.


const BarChart1 = props => {
  const { barData } = props;
  console.log(props.barData); //Why do I have to access props.barData instead of just barData?

  return (

Invariant Violation: Objects are not valid as a React child (found: object with keys {LEASED, RENTED, GIVEN BY GOVERNMENT, NA}). If you meant to render a collection of children, use an array instead.

But props.barData IS an array…isn’t it?

Then I looked up answers on StackOverflow and all their solutions seem to treat the data like an array:

Same with the suggested solution here: How do i render an array of object in react

And sure enough, when I try to use the FCC solution:
<p>{ => Object.keys(eachObj))}</p>

I get this error message:
TypeError: Cannot read property 'map' of undefined

This makes sense since the first error already told me to not treat this like an array.


(Also, secondary issue: when I try to assign props to const barData and then reference that constant (in console.log or <p></p>, I get empty logs or paragraphs. What am I missing…isn’t that how we’re supposed to use props? Assign to const then use further down in child component. This is a somewhat secondary problem but would appreciate the help.))

Judging from your errors, you’re sending to the child component only the first item of the array - React can’t render objects and objects don’t have map() method.

But - when I console.log the props in the child component, I see the array (the same one that I screenshot-ed above). Can console.log return something different from what child component “sees”…?

It would be better if you post your code on codesandbox or codepen.

1 Like

Here, thanks to your destructuring, props.barData === barData. If they appear to be different, something very weird is going on.

Based on the error message, it looks like it’s not — it’s an object with the stated keys (presumably the first item of that array, i.e. whateverYourArrayVarIsCalled[0]).

Overall, I’m not exactly sure what you’re trying to do. Are you trying to achieve something like this?

1 Like

Thanks @lionel-rowe. I think that is closer to what I need. However, when I tried to implement it in my own code, I got more errors. Rather than continue describing what I am seeing, I want to move my code to CodeSandBox and just show it to everyone. (As also suggested by @jenovs) . However, when it rains, it pours and now I am having issues with CodeSandBox. :sob:

All to say, I am at my day job at the moment but will work on getting the code up on CodeSandBox as soon as I can and then be back to share the link with y’all.

Thank you!

@lionel-rowe @jenovs - I have uploaded my code:

I am trying to follow one suggested solution, at least up to the point where I am reading the data in successfully and rendering a <p></p> for each member of array. Not worrying about the bar chart for now.

However, I continue to get the error: Cannot read property 'map' of undefined

In readData.jsx you should return allHHData from countAllHHData function instead of setting state (Line 140)

I made those changes. And the data is still being passed down to child component. However, the x-axis gets filled with (seemingly arbitrary numbers) and the y-axis is set to NaN.

I also went ahead and put the raw data in directly to the JSX element, <VictoryBar/> just to make sure it was working and it looks like it is.

Updated code:

I am posting this to Victory support forum too.