1- import { useCallback , useEffect , useMemo , useRef , useState , type Dispatch , type SetStateAction } from 'react' ;
1+ import { useCallback , useEffect , useMemo , useState , type Dispatch , type SetStateAction } from 'react' ;
22import { createPropagation } from 'use-propagate' ;
33import { useRefFrom } from 'use-ref-from' ;
44
5+ import useRefWithInit from './useRefWithInit' ;
6+
57export default function useStableStateHook < T > ( value : T ) : ( ) => readonly [ T ] ;
68
79export default function useStableStateHook < T > (
@@ -13,13 +15,9 @@ export default function useStableStateHook<T>(
1315 value : T ,
1416 setValue ?: Dispatch < SetStateAction < T > > | undefined
1517) : ( ) => readonly [ T , Dispatch < SetStateAction < T > > ] | readonly [ T ] {
16- const propagationRef = useRef < ReturnType < typeof createPropagation < T > > > ( ) ;
18+ const propagationRef = useRefWithInit < ReturnType < typeof createPropagation < T > > > ( ( ) => createPropagation < T > ( ) ) ;
1719 const valueRef = useRefFrom ( value ) ;
1820
19- if ( ! propagationRef . current ) {
20- propagationRef . current = createPropagation < T > ( ) ;
21- }
22-
2321 const {
2422 current : { usePropagate, useListen }
2523 } = propagationRef ;
@@ -28,6 +26,7 @@ export default function useStableStateHook<T>(
2826
2927 useEffect ( ( ) => propagate ( value ) , [ propagate , value ] ) ;
3028
29+ // One-off variable to hack around ESLint rules without disabling react-hooks/rules-of-hooks.
3130 const useHook = ( ) => {
3231 const [ propagatedValue , setPropagatedValue ] = useState < T > ( valueRef . current ) ;
3332
0 commit comments