Persistent data with Jotai #350
-
I'm writing a toy react app, found using jotai to hold values accessible from a different components pretty intuitive even I'm newbie. I need changed values to persist during page reload and all subsequent visits, I think indexdb/localstorage is the way to achieve this, so how can I get values from indexdb/localstorage everytime and store in atoms when the app launched? |
Beta Was this translation helpful? Give feedback.
Answered by
Aslemammad
Mar 11, 2021
Replies: 1 comment 2 replies
-
Hello @grubeli, You can check this. const strAtom = atom(localStorage.getItem('myKey') ?? 'foo')
const strAtomWithPersistence = atom(
(get) => get(strAtom),
(get, set, newStr) => {
set(strAtom, newStr)
localStorage.setItem('myKey', newStr)
}
) |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @grubeli, You can check this.