-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0b4436
commit 2db1f06
Showing
4 changed files
with
16 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
/** | ||
* @file useEventCallback shim from the latest React RFC. | ||
* @file useEventCallback shim | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import * as React from 'react' | ||
|
||
import * as syncRef from './syncRef' | ||
|
||
/** | ||
* useEventCallback shim from the latest React RFC. | ||
* useEvent shim. | ||
* @see https://github.com/reactjs/rfcs/pull/220 | ||
* @see https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md#internal-implementation | ||
*/ | ||
export function useEventCallback< | ||
Func extends (...args: Args) => Result, | ||
Args extends Parameters<any> = Parameters<Func>, | ||
Result extends ReturnType<any> = ReturnType<Func>, | ||
>(callback: Func): (...args: Args) => Result { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useEventCallback<Func extends (...args: any[]) => unknown>(callback: Func) { | ||
const callbackRef = syncRef.useSyncRef(callback) | ||
|
||
// make sure that the value of `this` provided for the call to fn is not `ref` | ||
// eslint-disable-next-line no-restricted-syntax | ||
return React.useCallback((...args) => callbackRef.current.apply(void 0, args), []) | ||
// Make sure that the value of `this` provided for the call to fn is not `ref` | ||
// This type assertion is safe, because it's a transparent wrapper around the original callback | ||
// eslint-disable-next-line react-hooks/exhaustive-deps, no-restricted-syntax | ||
return React.useCallback(((...args) => callbackRef.current.apply(void 0, args)) as Func, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters