Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[BUG]: AtomWithStorage should not return initialvalue #2617

Closed
Pszz opened this issue Jun 14, 2024 · 0 comments
Closed

[BUG]: AtomWithStorage should not return initialvalue #2617

Pszz opened this issue Jun 14, 2024 · 0 comments

Comments

@Pszz
Copy link

Pszz commented Jun 14, 2024

Summary

// When the user is not logged in, I set his sign to empty
const UserSign = atomWithStorage<string | null>('user_sign', null)

// hooks
const useLogin = () => { 
   const [sign, setSign] = useAtom(UserSign)
   console.log('sign::', sign)
   
   const login = async () => {
      await getUserToken()
      setSign('user sign token')
   }  
  
   useEffect(() => {
    // It always triggers the first time. 
    // In my opinion, when there is no content in storage, 
    // it has an initial value, and when there is content, the initial value should not be returned.

     if(sign === null) {
       login()
   }

}, [sign])
}

Other

  1. initialValue in atomWithStorage should not be required.
  2. The third parameter is not required either.
  3. I can't get around the second point when I only need to modify getOnInit:true in options.
@pmndrs pmndrs locked and limited conversation to collaborators Jun 18, 2024
@dai-shi dai-shi converted this issue into discussion #2622 Jun 18, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant