DEV Community


Discussion on: Why pressing the key works only once in my React project?

joelnet profile image
JavaScript Joel

This looks like an issue with stale values inside of useEffect.

Make this change:

// ❌ Stale value
if (e.code === 'Space') setItWorks(!itWorks)

// ✅ Correct value
if (e.code === 'Space') setItWorks(state => !state)
Enter fullscreen mode Exit fullscreen mode

I have created a video on this topic here:

Cheers 🍻

ptifur profile image
Yar Author

Thanks Joel! :)