-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use lazy refs for Autocomplete plugins (#9)
This uses lazy refs instead of standard refs to lazily instantiate plugins and ensure they only get called once. This method is recommended over `useMemo` because it's not designed to retain values, and could recompute arbitrarily in the future (see facebook/react#14490 (comment)).
- Loading branch information
1 parent
d8005d7
commit f97f9f4
Showing
3 changed files
with
20 additions
and
5 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useLazyRef'; |
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'; | ||
|
||
export function useLazyRef<TValue>(initialValue: () => TValue) { | ||
const ref = useRef<TValue | null>(null); | ||
|
||
return function getRef() { | ||
if (ref.current === null) { | ||
ref.current = initialValue(); | ||
} | ||
|
||
return ref.current; | ||
}; | ||
} |