Skip to content

Instantly share code, notes, and snippets.

@ynifamily3
Created May 19, 2022 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynifamily3/f74e54145e42509a9421a171d0eba3ef to your computer and use it in GitHub Desktop.
Save ynifamily3/f74e54145e42509a9421a171d0eba3ef to your computer and use it in GitHub Desktop.
모바일웹 백버튼 UX 대응
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