import { useEffect , useRef , useState } from 'react' ; export const useStateWithCallback = < T > ( initialState ) : [ state : T , setState : ( updatedState : React . SetStateAction < T > , callback ? : ( updatedState : T ) => void ) => void ] => { const [ state , setState ] = useState < T > ( initialState ) ; const callbackRef = useRef < ( ( updated : T ) => void ) [ ] > ( [ ] ) ; const handleSetState = ( updatedState , callback ? ) => { if ( callback ) { callbackRef . current . push ( callback ) ; } setState ( updatedState ) ; } ; useEffect ( ( ) => { if ( callbackRef . current . length > 0 ) { for ( let callback of callbackRef . current ) { callback ( state ) ; } callbackRef . current = [ ] ; } } , [ state ] ) ; return [ state , handleSetState ] ; } ;