[BUG]: AtomWithStorage should not return initialvalue #2622
Answered
by
dai-shi
Pszz
asked this question in
Bug report
-
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
|
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Jun 19, 2024
Replies: 1 comment 2 replies
-
Hi, thanks for reporting. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
initialValue
is required for the case there's no item in the storage.I would do something like
('user_sign', undefined, { getOnInit: true })
.