I have a component that takes a size property which is used in an array to render a list of choices. I found that when the size is changed via setState() in the higher level component this doesn't propagate to PizzaComponent. I can tell this is because of the array; map() doesn't get called again. If it were just a templated value it would work.
Another way I have found to make it work is to make pizzaChoices part of the component's state and then overwrite the array. This requires a lot of duplicate code and seems overkill.
Is there a way to pass a dynamic reference in an array or make a state value dependent on another state value?
const PizzaComponent = (props) => {
const {
size
} = props
const pizzaChoices = [
{
value: 'cheese',
label: 'Cheese',
price: '10.00',
},
{
value: 'pepperoni',
label: 'Pepperoni',
price: size,
},
];
return (
{
pizzaChoices.map((choice, i) => (
<MyFancyComponent
key={`pizza-${choice.value}`}
label={choice.label}
price={choice.price}
value={choice.value}
/>
))
}
)
}