You can achieve this without using React logic at all, you can simply use CSS animations (like you are currently doing) and CSS pseudo-classes ( :hover in this case) instead of dynamic Change the element's class manually.
https://developer.mozilla.org/en-US/docs/Web/CSS/:hover
Please check if this works for you: How to play animation on hover and pause on hover inactive