How can we access the individual elements or values of a prop which is an array or object?

Question

In the context of this exercise, how can we access the individual elements or values of a prop which is an array or object?

Answer

When passing in a prop which is an array or an object, you can access the elements or values as you would normally.

For example, with a prop that is an array, you can access an element using the index as follows,

/* A component with an array as a prop */
<SomeComponent arr={["A", "B"]} />

/* To access the second element of the array */
this.props.arr[1]

If the prop is an object, you can access a specific value using the corresponding key,

/* A component with an object as a prop */
<SomeComponent obj={{key: "value"}} />

/* To access the value of the key */
this.props.obj["key"]
12 Likes