Skip to content

Commit

Permalink
fix(useEventListener): change the way listener added and removed (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi authored Dec 20, 2022
1 parent a04803e commit f2936a5
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/useEventListener/useEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { RefObject, useEffect, useMemo } from 'react';
import { useIsMounted } from '../useIsMounted/useIsMounted';
import { useSyncedRef } from '../useSyncedRef/useSyncedRef';
import { hasOwnProperty } from '../util/misc';
import { hasOwnProperty, off, on } from '../util/misc';

/**
* An HTML element or ref object containing an HTML element.
Expand Down Expand Up @@ -53,10 +53,10 @@ export function useEventListener<T extends EventTarget>(
const restParams = params.slice(2);

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
tgt.addEventListener(params[0], eventListener, ...restParams);
on(tgt, params[0], eventListener, ...restParams);

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return () => tgt.removeEventListener(params[0], eventListener, ...restParams);
return () => off(tgt, params[0], eventListener, ...restParams);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target, params[0]]);
}

0 comments on commit f2936a5

Please sign in to comment.