Next.js Discord

Discord Forum

Wait while read value from local storage

Unanswered
Yellowfin tuna posted this in #help-forum
Open in Discord
Yellowfin tunaOP
Hello, I am using local storage to save user preferred value state. When page loads/reloads, firstly shown initial value provided to useLocalStorage hook. In the video you can see that Switch toggles on reload. How to fix this?

"use client";

import { title } from "@/components/primitives";
import { Switch } from "@nextui-org/switch";
import { useLocalStorage } from "@react-hooks-library/core";

export default function HelpPage() {
  const [isSelected, setIsSelected] = useLocalStorage("is-selected", false);

  return (
    <div className="flex flex-col gap-2">
      <Switch isSelected={isSelected} onValueChange={setIsSelected}></Switch>
      {isSelected ? "Sidebar is visible" : "Sidebar is not visible"}
    </div>
  );
}

0 Replies