Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When NOT to use useEvent

React 18 introduced the new useEvent hook.

This new hook seems so great that i'm wondering when NOT to use it ?

Appart from the case where i need to explicitly control the referential stability of a callback (and in that case i would use useCallback), i do not see any situation where useCallback should be preferred over useEvent.

Do you know any "cons" about using useEvent over useCallback ?

Can i safely convert (nearly) all my useCallback to useEvent ?

Thanks !

like image 841
Clement Avatar asked Sep 11 '25 14:09

Clement


1 Answers

Note: useEvent is not part of React at the moment. They are simply planing to add the hook in the future. (As of React 18.2.0.)


This is answered in the RFC for useEvent:

Some functions need to be memorized but are used during rendering. useCallback works for these cases. [...] Since useEvent functions throw if called during render, this isn't much of a pitfall.

There are several more examples in the RFC, I don't want to copy all of them here.

like image 189
asynts Avatar answered Sep 14 '25 03:09

asynts