The welcome page should ONLY appear during the first visit to the site
Accordingly, I used the useEffect hook, inside it I define the LocalStorage constant. I also use setTimeout to make the greeting disappear after a certain amount of time.
const [showStartPage, setShowStartPage] = useState(startPage.nothingShow);
useEffect(() => {
const firstTimeLoaded = localStorage.getItem("firstTimeLoaded")
if (!firstTimeLoaded) {
setTimeout(() => {
setShowStartPage(startPage.startPageShow);
localStorage.setItem("firstTimeLoaded", "true");
}, 2500);
} else setShowStartPage(startPage.startPageNotShow)
}, []);
<Fragment>
<Stack>
CONTENT ==>
</Stack>
{showStartPage === 2 && (<StartPage />)}
</Fragment>
==> Here is the content that is located under the welcome page during the first visit to the site.
To determine the state of the welcome page, I use an enum that includes 3 states:
- when the welcome page is shown
- when the welcome page is not shown
- when the state is not yet defined
export enum startPage {
startPageShow = 2,
startPageNotShow = 1,
nothingShow = 0
}
As a result, the welcome page is not loaded when you re-enter the site, as it should be. Loads the first time you visit the site, as it should be. But it does not disappear after a while, as it should NOT.
What did I do wrong?
I tried to simplify the situation by excluding the enum and leaving only boolean values, but I need an undefined state to not show anything until the state is defined
2
Answers
You can use an initializer function to get the
showStartPage
state from local storage. If the key doesn’t exist in local storage (orfalse
), show the start page, and trigger thesetTimeout
in theuseEffect
to hide it.