I'm passing an array as props from the App.js to Component B. The array:
myOptions: [
{
id: 1,
option: "Option1"
},
{
id: 2,
option: "Option2"
},
{
id: 3,
option: "Option3"
}
]
From the App.js I'm passing it to Component B:
<ComponentB
options={this.state.myOptions}
/>
ComponentB:
import React from 'react';
import ComponentC from './ComponentC';
function ComponentB (props) {
function renderOptions(key) {
return(
<ComponentC
key={key.option}
optionContent={key.option} //this is my question
answers={props.option}
/>
);
}
return (
<div>
<h3> ComponentB </h3>
{props.options.map(renderOptions)}
</div>
);
}
export default ComponentB;
The ComponentC only displays the optionContent:
import React from 'react';
function ComponentC(props) {
return (
<div>
{props.optionContent}
</div>
);
}
export default ComponentC;
Although the code works correctly, I'm not sure why in the renderOptions
function, I need to use key.option
to pass the optionContent={key.option}
prop. When I use props.option
, it doesn't work.
Is it because the renderOptions
function is nested in the ComponentB
function?