Created
May 19, 2022 01:14
-
-
Save ynifamily3/f74e54145e42509a9421a171d0eba3ef to your computer and use it in GitHub Desktop.
모바일웹 백버튼 UX 대응
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
import { useCallback, useEffect, useRef } from 'react'; | |
export function useSupportBack(listening: boolean, onBack: () => void) { | |
const fnRef = useRef(onBack); | |
const memoizedCallback = useCallback(() => { | |
fnRef.current(); | |
window.removeEventListener('popstate', memoizedCallback); | |
}, []); | |
useEffect(() => { | |
fnRef.current = onBack; | |
}); | |
useEffect(() => { | |
if (!listening) return; | |
window.history.pushState(null, ''); | |
window.addEventListener('popstate', memoizedCallback); | |
return () => { | |
window.removeEventListener('popstate', memoizedCallback); | |
}; | |
}, [listening]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment