Handle React state inside an event listener callback

Without recreating your function on every render.

Jose Antonio Gonzalez Rodriguez
Level Up Coding
Published in
3 min readNov 24, 2021

--

Photo by Erik-Jan Leusink on Unsplash

The other day my girlfriend danwessels was working on a feature where you could navigate a carousel of images using your keyboard arrows. The carousel already had this functionality with UI arrows but humans are lazy and don’t want to move the cursor and click on buttons. A simple enough

--

--

Mexican living in Porto Mexican living in Porto helping feed the world sustainably.