import { UIEvent, useCallback, useEffect, useRef } from 'react'; /** The list of persisted scroll positions, by id */ const scrollPositions: Record = {}; /** * Keep track and restore the scroll position when changing page. * The ID is used to identify the stored scroll position. */ export function usePersistScroll(id: string) { const ref = useRef(null); const onScroll = useCallback((event: UIEvent) => { scrollPositions[id] = event.currentTarget.scrollTop; }, []); useEffect( function scrollRefDidMount() { if (ref.current && scrollPositions[id] > 0) { ref.current.scrollTop = scrollPositions[id]; } }, [ref.current] ); return { ref, onScroll }; }