I got a list with 2 react components and their initial color is black.
The objective is next , when I click on a component I want to set the state of the previous on black if it is orange.
I tried with useState hook but I didn’t have much success.
If someone could give me some hints it will be perfect.
const Square = () => {
const [actualColor, setColor] = useState("black")
const onClickEnterHandler = () => setColor("orange")
return(
<div>
</div>
)
}
export default Square
2
Answers
If you want to change multiple components, you have to move your state to their common parent and pass the state as prop or use React Context.
IF I got it correctly, when clicking on square 2, you want square 1 to be black and square 2 (currently active square) to be orange.
If so, then try:
Then in your Square component, you can conditionally check the
active
prop and set the active Square to orange.