这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ import { recoilPersist } from 'recoil-persist'
const { persistAtom } = recoilPersist({
key: 'recoil-persist', // this key is using to store data in local storage
storage: localStorage, // configurate which storage will be used to store the data
converter: { // configurate how values will be serialized/deserialized in storage
parse: (value) => JSON.parse,
stringify: (value) => JSON.stringify
}
})
```

Expand Down Expand Up @@ -176,6 +180,22 @@ type config.storage = Storage
Set `config.storage` with `sessionStorage` or other `Storage` implementation to
change storage target. Otherwise `localStorage` is used (default).

```js
type config.converter = {
stringify: (value: any) => string
parse: (value: string) => any
}
```

Set `config.converter` to an object which implements both `stringify` and `parse` functions to convert state values to and from strings. One use of this would be to wrap the standard `JSON.stringify` and `JSON.parse` functions, e.g. to insert your own `reviver` and `replacer` functions:

```js
{
parse: (value) => JSON.parse(value, myCustomReviver),
stringify: (value) => JSON.stringify(value, myCustomReplacer)
};
```

## Migration from version 1.x.x to 2.x.x

The API changed from version 1.x.x.
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ export interface PersistStorage {
getItem(key: string): null | string | Promise<string>
}

export interface PersistConverter {
stringify: (value: any) => string
parse: (value: string) => any
}

export interface PersistConfiguration {
key?: string
storage?: PersistStorage
converter?: PersistConverter
}

/**
Expand All @@ -27,7 +33,7 @@ export const recoilPersist = (
}
}

const { key = 'recoil-persist', storage = localStorage } = config
const { key = 'recoil-persist', storage = localStorage, converter = JSON } = config

const persistAtom: AtomEffect<any> = ({ onSet, node, trigger, setSelf }) => {
if (trigger === 'get') {
Expand Down Expand Up @@ -89,7 +95,7 @@ export const recoilPersist = (
return {}
}
try {
return JSON.parse(state)
return converter.parse(state)
} catch (e) {
console.error(e)
return {}
Expand All @@ -99,9 +105,9 @@ export const recoilPersist = (
const setState = (state: any): void => {
try {
if (typeof storage.mergeItem === 'function') {
storage.mergeItem(key, JSON.stringify(state))
storage.mergeItem(key, converter.stringify(state))
} else {
storage.setItem(key, JSON.stringify(state))
storage.setItem(key, converter.stringify(state))
}
} catch (e) {
console.error(e)
Expand Down