Question:
How to make only one button disabled on click in map method? With the help of the disabled hook, I have all pressed buttons . And how to make it so that after the timer expires the ‘current__events__hot-price disabled’ className changes back to ‘current__events__hot-price’?Answer:
From what I can gather, you toggle one element’s status by index and disable the button at that specific index. Instead of toggling thestatusItem
property (a state mutation, BTW) you should just store the index of the “active” item.Add an
activeIndex
state that is null in its “inactive” state, and equal to an index when it is “active”. The useEffect
hook should just instantiate the interval and the interval callback should just decrement the time left. Use a separate useEffect
hook with a dependency on the timeLeft
state to reset the activeIndex
and timeLeft
states. Disable the button element when the activeIndex
equals the mapped index. Pass the activeIndex
state to the CSS classname utility functions.Example:
If you have better answer, please add a comment about this, thank you!