MCPcopy Create free account
hub / github.com/streamich/react-use / useEvent

Function useEvent

src/useEvent.ts:35–61  ·  view source on GitHub ↗
(
  name: Parameters<AddEventListener<T>>[0],
  handler?: null | undefined | Parameters<AddEventListener<T>>[1],
  target: null | T | Window = defaultTarget,
  options?: UseEventOptions<T>
)

Source from the content-addressed store, hash-verified

33export type UseEventOptions<T> = Parameters<AddEventListener<T>>[2];
34
35const useEvent = <T extends UseEventTarget>(
36 name: Parameters<AddEventListener<T>>[0],
37 handler?: null | undefined | Parameters<AddEventListener<T>>[1],
38 target: null | T | Window = defaultTarget,
39 options?: UseEventOptions<T>
40) => {
41 useEffect(() => {
42 if (!handler) {
43 return;
44 }
45 if (!target) {
46 return;
47 }
48 if (isListenerType1(target)) {
49 on(target, name, handler, options);
50 } else if (isListenerType2(target)) {
51 target.on(name, handler, options);
52 }
53 return () => {
54 if (isListenerType1(target)) {
55 off(target, name, handler, options);
56 } else if (isListenerType2(target)) {
57 target.off(name, handler, options);
58 }
59 };
60 }, [name, handler, target, JSON.stringify(options)]);
61};
62
63export default useEvent;

Callers 4

DemoFunction · 0.90
useKeyFunction · 0.85
checkOnMountAndUnmountFunction · 0.85
checkOnDepsChangesFunction · 0.85

Calls 6

onFunction · 0.90
offFunction · 0.90
isListenerType1Function · 0.85
isListenerType2Function · 0.85
onMethod · 0.80
offMethod · 0.80

Tested by 2

checkOnMountAndUnmountFunction · 0.68
checkOnDepsChangesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…