You can choose cookie or localStorage for popup.
참고 자료
reference-1
reference-2
reference-3
// index.js
import React, { useState, useEffect } from 'react';
import Modal from 'components/Modal/Modal';
const Home = () => {
const [showModal, setShowModal] = useState(false);
const HAS_VISITED_BEFORE = localStorage.getItem('hasVisitedBefore');
useEffect(() => {
const handleShowModal = () => {
if (HAS_VISITED_BEFORE && HAS_VISITED_BEFORE > new Date()) {
return;
}
if (!HAS_VISITED_BEFORE) {
setShowModal(true);
let expires = new Date();
expires = expires.setHours(expires.getHours() + 24);
localStorage.setItem('hasVisitedBefore', expires);
}
};
window.setTimeout(handleShowModal, 2000);
}, [HAS_VISITED_BEFORE]);
const handleClose = () => setShowModal(false);
return (
{showModal && <Modal onClose={handleClose} />}
// ...
)
};
export default Home;
한눈에 알아보기 좋은 코드네요! 참고 많이 되었습니다 감사합니다