I have a parent component which is created through using another component by iterating over a for loop n number of times, and an id is assigned to each child.
Now, I need to change the state of each child from within the parent. But each individual piece has to be changed one at a time, depending on the input.
How can I achieve this? Do I have to declare n number of state variables?
const Parent = () => {
const [color1, setColor1] = useState('yellow')
const [color2, setColor2] = useState('yellow')
function updateState1 () {
setColor1('red')
}
function updateState2 () {
setColor2('red')
}
return (
<>
<div>Parent</div>
<Child childState={color1} updateState={updateState1} id='1' />
<Child childState={color2} updateState={updateState2} id='2' />
</>
)
}
Below is the child component. I understand that the id is not doing anything here.
I want to be able to just have one function through which I can pass the id of the button I want to target
and the color i want to set it to.
const btnStyle = {
backgroundColor: 'green'
}
const Child = ({ childState, updateState, id, targetButton }) => {
function handleClick () {
updateState()
}
return (
<button id={id} style={((childState === null) && a==id) ? btnStyle: {"backgroundColor":childState}} onClick={handleClick}>
Click me
</button>
)
}
2
Answers
You can use an array or an object to store the state values for each child component and pass a function from the parent component to the child component that can update the state value based on the
id
orindex
of the child component.For example:
You can see the result of this example here: codesandbox.io
Make the child as simple as possible, and control the state and the state change in the parent: