Clement
Clement

Reputation: 4058

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 !

Upvotes: 4

Views: 2203

Answers (1)

asynts
asynts

Reputation: 2423

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.

Upvotes: 4

Related Questions