I'm trying to create a new dynamic component that receives a value from its parent and is added to an array. However, that component prop must update itself and change in all the components inside the array when the original value change, but use useState don“t change the value in the inside the component and only use the value that was added when was added to the array.
I need to know how to send the value to the child components that are constantly updated in all of them.
Here is my code:
parent:
import React, {useState} from 'react';
import Child from './TestComponentChild';
export default function Parent(){
const [var1, setVar1] = useState(0);
const [array, setArray] = useState([]);
return(
<div>
{array}
<button onClick={() => { setArray([...array , <Child var2={var1}/> ]) }}>TestButton1</button>
<button onClick={() => { setVar1(var1 + 1)}}>TestButton2</button>
</div>
)
}
Child
export default function Child(props){
return(
<div>
{props.var2}
</div>
)
}