Next.js Discord

Discord Forum

Flickering state render

Unanswered
Yellow-bellied Flycatcher posted this in #help-forum
Open in Discord
Yellow-bellied FlycatcherOP
Hi, I‘m trying to render a component depending on the user agent. If the user agent is flutter, I want to hide the component. This works fine.

The problem is that the component flickers on other user agents than flutter because the user agent is requested delayed (so the component appears some milliseconds later on the page). How to prevent the flickering so that the component is rendered without the user noticing?

import React, { useEffect, useState } from "react";
// ... Imports ...

export default function Header({ fixed }) {
    const [isInApp, setIsInApp] = useState(null); 
    const { t } = useTranslation("common");
    const { user, isLoggedIn, hasRole } = useUser();
    const router = useRouter();

    useEffect(() => {
        if (typeof navigator !== "undefined") {
            const isFlutter = navigator.userAgent.includes("Flutter");
            setIsInApp(isFlutter);
        }
    }, []);

    if (isInApp === null && typeof window === "undefined") {
        return null;
    }

    if (isInApp === true) {
        return null;
    }

    return (
        <header className={`${fixed ? "sticky-header" : ""}`}>
            lt ... */}
        </header>
    );
}

3 Replies

Yellow-bellied FlycatcherOP
Still Needing help
there is ideally no best way than this
@Yellow-bellied Flycatcher Still Needing help
California pilchard
THe only way is to add a global loading state but not ideal I guess