Original fishing-dog mascot for jaspr_hooks jaspr_hooks

useEvent

Keep callback identity stable while invoking the latest handler.

useEvent returns a stable one-argument callable whose implementation is refreshed on every build. Use a record when the event needs multiple arguments.

Signature

StableEventCallback<T, R> useEvent<T, R>(R Function(T value) handler);

Lifecycle

final onValue = useEvent<int, void>((value) => save(value, currentUser));
Interactive useEvent demo
Multiplier 2 • result 0

The returned object's identity remains stable until unmount, while calls always reach the newest closure.

Server rendering

The callable is deterministic and usable on the server. It owns no browser resources.

Common mistakes

Do not call the handler during build when it mutates state. Use useCallback when dependency changes should deliberately change callback identity.

Use useLatest for a stable mutable reference and useEventListener for native DOM subscriptions.