Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setUser not setting the value to the localStorage #38

Open
aynuayex opened this issue May 25, 2024 · 1 comment
Open

setUser not setting the value to the localStorage #38

aynuayex opened this issue May 25, 2024 · 1 comment

Comments

@aynuayex
Copy link

Describe the bug
i have just copied from the docs directly and used it like below the the user key value is not set on the localStorage, and i do not even think this library works?
here is the version "use-local-storage": "^3.0.0",
here is the code

export default function Login() {
const [user, setUser] = useLocalStorage("user",{});

async function onSubmit(values: z.infer<typeof formSchema>) {
    try {
      const { userId, password } = values;
      const response = await axios.get(
        `${
          import.meta.env.VITE_BASE_API
        }/Login/SignIn?UidCod=${userId}&UidPwd=${password}`
      );
      console.log(response.data)
      if (response.statusText === "OK") {
        setUser(response.data);
        navigate("/out_patient");
        return;
      }
    } catch (err) {
      console.log(err);
    }
  }
return (<>It does not work I should just stick to the localStorage API or built my own or use another one!</>);
}
@nas5w
Copy link
Owner

nas5w commented May 25, 2024

Hi @aynuayex, sorry the package isn't working for you. It does work, but possibly not as you expect.

I suspect the navigate function you're using is unmounting the component before state is actually set. In this library, the local storage value is set in an effect once the state is set. You could validate this is the cause by removing the navigate call and seeing if local storage is set.

If this ends up being the issue, you have a couple options. If you want to keep using this package, I believe the following should work:

const emptyUser = {};
export default function Login() {
const [user, setUser] = useLocalStorage("user", emptyUser);

useEffect(() => {
  if (user !== emptyUser) {
        navigate("/out_patient");
  }
}, [user])

async function onSubmit(values: z.infer<typeof formSchema>) {
    try {
      const { userId, password } = values;
      const response = await axios.get(
        `${
          import.meta.env.VITE_BASE_API
        }/Login/SignIn?UidCod=${userId}&UidPwd=${password}`
      );
      console.log(response.data)
      if (response.statusText === "OK") {
        setUser(response.data);
        return;
      }
    } catch (err) {
      console.log(err);
    }
  }
return (<>It does not work I should just stick to the localStorage API or built my own or use another one!</>);
}

If you'd like to look into another library that might synchronously set the local storage value, consider checking out use-local-storage-state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants