Next.js Discord

Discord Forum

Cookie handling on client components

Unanswered
LeonTM posted this in #help-forum
Open in Discord
Hey, so I am trying to implement a update popup for my dashboard application. I am using this logic to figure out what version update the client already read. But on the way I found that this might be delaying certain parts and I dont know why
´´´const [display, setDisplay] = React.useState(false);
React.useEffect(() => {
const dismissedVersion = document.cookie.split("; ").find(row => row.startsWith("dismissedVersion="))?.split("=")[1];

if (dismissedVersion === textVersion) {
setDisplay(false);
} else {
setDisplay(true);
}
}, [])
```

2 Replies

const [display, setDisplay] = React.useState(false);
    React.useEffect(() => {
        const dismissedVersion = document.cookie.split("; ").find(row => row.startsWith("dismissedVersion="))?.split("=")[1];

        if (dismissedVersion === textVersion) {
            setDisplay(false);
        } else {
            setDisplay(true);
        }
    }, [])
Here is the code again, so it turns the update notification down if it registers the correct cookie. I've tried using js-cookie, but this had a slight delay too. I am not sure where this delay is coming from, but I am almost certain it comes from this logic, because every aspect of my application that isnt fully precoded (example for something precoded would be the navbar contents like the links to other pages) and therefore is being rendered when an API returns some data (for example the username and their iconUri of the currently logged in user) is coming in at the exact same time, even if the requests are slightly faster or slower than the registration of the cookie. I feel like nextjs waits until it has gotten the cookie to load all the incoming data at once. Is that normal and if not is it even possible to have such loading behaviours?