I have a component (which renders a list element). When clicked a state isActive
in that component will be set to true
. But I want the other components isActive
state to be set to false. But I can't seem to figure out how to set the state of a different component when interacting with an component.
https://codesandbox.io/s/eloquent-elbakyan-5powe?file=/src/App.tsx
export const ListElement = () => {
const [isActive, setIsActive] = React.useState(false)
const handleClick = () => {
setIsActive(!isActive)
}
return (
<li
onClick={handleClick}
className={isActive ? 'active' : ''}
>
toggle between these elements
</li>
)
}