---
title: Persistence
description: How to persist atoms
nav: 3.03
---
Jotai has an [atomWithStorage function in the utils bundle](../api/utils.mdx#atom-with-storage) for persistence that supports persisting state in `sessionStorage`, `localStorage`, `AsyncStorage`, or the URL hash.
There are also several alternate implementations here:
## A simple pattern with localStorage
```js
const strAtom = atom(localStorage.getItem('myKey') ?? 'foo')
const strAtomWithPersistence = atom(
(get) => get(strAtom),
(get, set, newStr) => {
set(strAtom, newStr)
localStorage.setItem('myKey', newStr)
}
)
```
## A helper function with localStorage and JSON parse
```js
const atomWithLocalStorage = (key, initialValue) => {
const getInitialValue = () => {
const item = localStorage.getItem(key)
if (item !== null) {
return JSON.parse(item)
}
return initialValue
}
const baseAtom = atom(getInitialValue())
const derivedAtom = atom(
(get) => get(baseAtom),
(get, set, update) => {
const nextValue =
typeof update === 'function' ? update(get(baseAtom)) : update
set(baseAtom, nextValue)
localStorage.setItem(key, JSON.stringify(nextValue))
}
)
return derivedAtom
}
```
(Error handling should be added.)
## A helper function with AsyncStorage and JSON parse
This requires [onMount](../api/core.mdx#on-mount).
```js
const atomWithAsyncStorage = (key, initialValue) => {
const baseAtom = atom(initialValue)
baseAtom.onMount = (setValue) => {
;(async () => {
const item = await AsyncStorage.getItem(key)
setValue(JSON.parse(item))
})()
}
const derivedAtom = atom(
(get) => get(baseAtom),
(get, set, update) => {
const nextValue =
typeof update === 'function' ? update(get(baseAtom)) : update
set(baseAtom, nextValue)
AsyncStorage.setItem(key, JSON.stringify(nextValue))
}
)
return derivedAtom
}
```
Don't forget to check out the [Async documentation](./async) for more details on how to use async atoms.
### Usage in Async actions
When using an asynchronous storage, the reading of the created atoms are asynchronous too. That comes with some limitations. Before `useAtom` has been called at least once, the value is not set.
This can cause an issue, for example, if you want to retrieve the value of your stored atom in an async action (write-only atom).
Let's use the [atomWithStorage function in the utils bundle](../api/utils.mdx#atom-with-storage) for the examples, so that we have more options available.
```js
const storage = createJSONStorage(() => AsyncStorage)
const userId = atomWithStorage('user-id-key', null, storage)
const userInfo = atom({})
const fetchUserInfo = atom(null, async (get, set, payload: any) => {
const id = get(userId) // This throws an error if "userId" is not loaded yet
const info = await fetch('https://jotai.org/users/' + id)
set(userInfo, info)
})
```
We want to make sure the action will never fail. As always with asynchronous flow in jotai, we have 2 options: with or without **Suspense**.
#### With Suspense
You may want to preload atoms at root level of your app directly:
```js
const Preloader = () => {
useAtomValue(userId) // Trigger the "onMount" function that will load the data from the store
return null
}
```
```js
const Root = () => {
return (