React PopUp Feature
웹사이트 접근 > 팝업 등장 > [로컬스토리지 push > 팝업 지속X > Date 함수 > 일정 시간 지난 후 > 팝업 다시 등장]
웹사이트 접근
팝업 등장
const HAS_VISITED_BEFORE = localStorage.getItem("hasVisitedBefore");
const [popup, setPopup] = useState(false);
useEffect(() => {
const handleShowModal = () => {
if (HAS_VISITED_BEFORE && HAS_VISITED_BEFORE > new Date()) {
return;
}
if (!HAS_VISITED_BEFORE) {
setPopup(true);
let expires = new Date();
expires = expires.setHours(expires.getHours() + 3);
localStorage.setItem("hasVisitedBefore", expires);
}
};
window.setTimeout(handleShowModal, 1000);
}, [HAS_VISITED_BEFORE]);
const onCanelHandler = () => {
setPopup(false);
};
팝업 만들기
<Modal
visible={popup}
onCancel={onCanelHandler}
centered
footer={null}
className="popup"
width="initial"
>
<a href="/setting">
<img src="/images/popup.png" alt="popup" />
</a>
</Modal>