i want to be able to toggle class "active" on/off on a single item of a list. Say we have a list of elements:
<ul>
<li><a href="#" className="list-item">First</a></li>
<li><a href="#" className="list-item">Second</a></li>
<li><a href="#" className="list-item">Third</a></li>
</ul>
and I want to be able to add class "active" on a second element on click. I cannot use state for that, because it would change the classes of other 2 elements, if we add the condition there, right ?
So the solution could be to create a sub-component say <ListItem /> that could have its own state and own onclick method, that would change only his own class. But then, when we want to remove class "active" on other elements on click, this sub-component would have to dispatch method to a parent to remove class from other elements. It seems pretty complicated for a task of a easy kind ( you know the 10secs-in-jquery kind).
Does anyone have an easier solution for this ?
It would be very nice to not have to use another big npm module for this, document.getElementsByClassName (obviously) nor refs.
because it would change the classes of other 2 elementswhat do you mean by this? you wouldn't have them all change on a state change. and even if they did this wouldn't be a performance issue. Not sure what the problem is.