Skip to content

Commit

Permalink
Add useInitRef
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Aug 21, 2023
1 parent 6bbabbf commit 648ccac
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/hooks/useInitRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MutableRefObject, useRef } from 'react';

export function useInitRef<T>(initializer: () => T): MutableRefObject<T> {
const ref = useRef<T | null>(null);
if (ref.current === null) {
ref.current = initializer();
}
return ref as MutableRefObject<T>;
}

0 comments on commit 648ccac

Please sign in to comment.