-3

I have a component who receive props:

The data recived printed on console.log

enter image description here

How to extract the array from this object?

Before I send the array to my component look like this:

enter image description here

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
Kentron.dna
  • 183
  • 11

3 Answers3

0

If you are doing

console.log(props);

you can get the array with:

const arr = props.data;

or

const { data } = props;

Davide Carpini
  • 1,581
  • 17
  • 15
0

I have finally used the function map() to resolve this issue.

Kentron.dna
  • 183
  • 11
-1

It sounds like you have a React component that is receive data like this:

<MyComponent data={myArray} />

If so, then inside that component, it will receive it on the prop named data.

function MyComponent(props) {
  console.log(props.data)
  return <p>{data}</p>
}

Or written with the more common destructuring assignment:

function MyComponent({data}) {
  console.log(data)
  return <p>{data}</p>
}
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • So how's `console.log(props.data)` not covered by the duplicate? – Jonas Wilms Apr 03 '20 at 22:30
  • @JonasWilms According to the questioner, a component receives props `{ data: myArray }`, but the data started as just `myArray`. So in addition to explaining how to get that data, I believed it worth explaining where the `data` property on this object came from, since that seemed unclear to the questioner, especially considering their apparent level of expertise. So I thought a generic answer about how to get a property from a variable might be inadequate. I sometimes see questions closed as duplicates that fail to adequately connect the questioners code to the solution cleanly. – Alex Wayne Apr 03 '20 at 22:49