forked from zhihu/griffith
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use hooks in PositionProvider
- Loading branch information
Showing
4 changed files
with
94 additions
and
103 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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {useRef} from 'react' | ||
|
||
/** | ||
* Create immutable function ref | ||
*/ | ||
const useHandler = <T extends (...args: any[]) => any>(handler: T) => { | ||
const handlerRef = useRef<T>(handler) | ||
handlerRef.current = handler | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return useRef(((...args: any[]) => handlerRef.current(...args)) as T).current | ||
} | ||
|
||
export default useHandler |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {useRef, useEffect} from 'react' | ||
|
||
/** | ||
* Get previous rendered value | ||
* | ||
* @see https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state | ||
*/ | ||
function usePrevious<T>(value: T) { | ||
const ref = useRef<T>() | ||
|
||
useEffect(() => { | ||
ref.current = value | ||
}, [value]) | ||
|
||
return ref.current | ||
} | ||
|
||
export default usePrevious |