Combines Recoil with persistent storage, such as Browser LocalStorage and React Native async storage.
npm install recoil-persistence 
Persist your Recoil states between site/app loads. Written in Typescript with detailed type declarations.
Supports React and React Native.
Please note that Recoil Atom Effects are experimental and the API is still evolving, hence they are used as effects_UNSTABLE. Due to the fact that the syntax could completely change at any moment, this repo might not always work with the latest verison of recoil
Current known working version of Recoil: 0.2.0
- API
- React
- persistentAtom
- atomOptions
- persistOptions
- Examples
- storageEffect
- Examples
- React Native
- persistentAtom
- atomOptions
- persistOptions
- storage
- Examples
- storageEffect
- Examples
We have support for React and React Native. You should only use the version you need.
There are minor differences between both versions, so read the documentation for the specific version you are using.
``ts
// React
import { persistentAtom } from 'recoil-persistence/react'
// React Native
import { persistentAtom } from 'recoil-persistence/react-native'
`
#### persistentAtom
A drop-in replacement for Recoil's atom.
Accepts two parameters: atomOptions and persistOptions.
##### atomOptions
atomOptions holds the options you'd normally pass to atom(), including your key and default.
##### persistOptions
persistOptions holds options relating to state persistence. These options are all optional:
- persistOptions.key allows you provide a custom key to use for the state in storage, otherwise we default to the atom's key.persistOptions.storage
- allows you to override the default Storage location of localStoragepersistOptions.validator
- a function to check that the data from Storage is valid.
We don't perform any validation on the returned string from storage. Please do this yourself via the validator option as users can easily edit LocalStorage and SessionStorage.
##### Examples
`ts
// Default options
const todoListState = persistentAtom({
key: 'todoListState',
default: [],
})
// Customised options
const todoListState_custom = persistentAtom(
{
key: 'todoListState',
default: [],
},
{
key: 'todo-state',
storage: SessionStorage,
validator: data => data !== null, // Won't load the data if it's equal to null
},
)
`
#### storageEffect
A Recoil atom effect that performs the persistence.
If you don't want to use the atom wrapper for some reason, you can use this instead.
storageEffect accepts three parameters:
- key The key that the data is stored under in Storage.storage
- (Optional) Allows you to choose between window.localStorage (default) and window.sessionStorage.validator
- (Optional) A function to check that the data from Storage is valid.
We don't perform any validation on the returned string from storage. Please do this yourself via the validator parameter as users can easily edit LocalStorage and SessionStorage.
##### Examples
`tsmy-state-in-LS
// Stores the value of state in LocalStorage under key
atom
key: 'myState',
default: { data: null },
effects_UNSTABLE: [storageEffect
})
// Stores the value of state in SessionStorage under my-state2-in-SS key
atom
key: 'myState2',
default: { data: null },
effects_UNSTABLE: [storageEffect
})
// Stores the value of state in LocalStorage (default) under my-state3-in-LS key`
atom
key: 'myState3',
default: { data: null },
effects_UNSTABLE: [
storageEffect
'my-state3-in-SS',
undefined,
data => data !== null, // Won't load the data if it's equal to null
),
],
})
#### persistentAtom
A drop-in replacement for Recoil's atom.
Accepts three parameters: atomOptions, persistOptions and storage.
##### atomOptions
atomOptions holds the options you'd normally pass to atom(), including your key and default.
##### persistOptions
persistOptions holds options relating to state persistence. These options are all optional:
- persistOptions.storageKey allows you provide a custom key to use for the state in storage, otherwise we default to the atom's key.persistOptions.validator
- a function to check that the data from Storage is valid.
We don't perform any validation on the returned string from storage. Please do this yourself via the validator option as users can easily edit LocalStorage and SessionStorage.
##### storage
You cna use this parameter to pass a custom storage system. This system should implement the IRNStorageSystem interface.
If this is missing or undefined, we'll use @react-native-async-storage/async-storage by default. Make sure this is installed as a dependency in your project.
`tsdata
const myStorageSystem = {
setItem: async (key, data) => {}, // set in storage system under keykey
getItem: async key => {}, // set data from storage system by key
removeItem: async key => {}, // remove data from storage system by `
}
##### Examples
`ts
// Default options
const todoListState = persistentAtom({
key: 'todoListState',
default: [],
})
// Customised options
const todoListState_custom = persistentAtom(
{
key: 'todoListState',
default: [],
},
{
storageKey: 'todo-state',
validator: data => data !== null, // Won't load the data if it's equal to null
},
myStorageSystem,
)
`
#### storageEffect
A Recoil atom effect that performs the persistence.
If you don't want to use the atom wrapper for some reason, you can use this instead.
storageEffect accepts three parameters:
- key The key that the data is stored under in the storage system.storage
- (Optional) Allows you to provide a custom storage system to persist state in.validator
- (Optional) A function to check that the data from Storage is valid.
We don't perform any validation on the returned string from storage. Please do this yourself via the validator parameter as users can easily edit LocalStorage and SessionStorage.
##### Examples
`tsmy-state-in-LS
// Stores the value of state in LocalStorage under key
atom
key: 'myState',
default: { data: null },
effects_UNSTABLE: [storageEffect
})
// Stores the value of state in SessionStorage under my-state2-in-SS key
atom
key: 'myState2',
default: { data: null },
effects_UNSTABLE: [storageEffect
})
// Stores the value of state in storage under my-state3-in-SS key@react-native-async-storage/async-storage
atom
key: 'myState3',
default: { data: null },
effects_UNSTABLE: [
storageEffect
'my-state3-in-SS',
undefined, // Use ``
data => data !== null, // Won't load the data if it's equal to null
),
],
})