부모 컴포넌트
const Parent = () => {
const [closeSession, setCloseSession] = useState(false);
const closeQuickView = () => {
setCloseSession(true);
};
useEffect(() => {
window.history.pushState('fake-route', document.title, window.location.href);
window.addEventListener('popstate', closeQuickView);
return () => {
window.removeEventListener('popstate', closeQuickView);
if (window.history.state === 'fake-route') {
window.history.back();
}
};
}, []);
return (
<Child closeSession={closeSession} />
);
};
자식 컴포넌트
const Child = ({ closeSession }) => {
useEffect(() => {
if (closeSession) {
doAnything();
}
}, [closeSession]);
};