Skip to content

Commit

Permalink
feat: useStorageState setStoreValue catch this error to the developer (
Browse files Browse the repository at this point in the history
…#2123)

* fix: useStorageState getStoreValue catch this error to the developer

* fix: replace throw with onError

---------

Co-authored-by: liuyib <1656081615@qq.com>
  • Loading branch information
51wangping and liuyib committed Mar 28, 2023
1 parent e3a7526 commit d5fd82c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
12 changes: 9 additions & 3 deletions packages/hooks/src/createUseStorageState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ export interface Options<T> {
serializer?: (value: T) => string;
deserializer?: (value: string) => T;
defaultValue?: T | IFuncUpdater<T>;
onError?: (error: unknown) => void;
}

export function createUseStorageState(getStorage: () => Storage | undefined) {
function useStorageState<T>(key: string, options?: Options<T>) {
function useStorageState<T>(key: string, options: Options<T> = {}) {
let storage: Storage | undefined;
const {
onError = (e) => {
console.error(e);
},
} = options;

// https://github.com/alibaba/hooks/issues/800
try {
storage = getStorage();
} catch (err) {
console.error(err);
onError(err);
}

const serializer = (value: T) => {
Expand All @@ -49,7 +55,7 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
return deserializer(raw);
}
} catch (e) {
console.error(e);
onError(e);
}
if (isFunction(options?.defaultValue)) {
return options?.defaultValue();
Expand Down
11 changes: 6 additions & 5 deletions packages/hooks/src/useLocalStorageState/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ const [state, setState] = useLocalStorageState<T>(
### Options
| Property | Description | Type | Default |
| ------------ | ----------------------------- | ------------------------ | ---------------- |
| defaultValue | Default value | `any \| (() => any)` | - |
| serializer | Custom serialization method | `(value: any) => string` | `JSON.stringify` |
| deserializer | Custom deserialization method | `(value: string) => any` | `JSON.parse` |
| Property | Description | Type | Default |
| ------------ | ----------------------------- | -------------------------- | ----------------------------- |
| defaultValue | Default value | `any \| (() => any)` | - |
| serializer | Custom serialization method | `(value: any) => string` | `JSON.stringify` |
| deserializer | Custom deserialization method | `(value: string) => any` | `JSON.parse` |
| onError | On error callback | `(error: unknown) => void` | `(e) => { console.error(e) }` |
## Remark
Expand Down
11 changes: 6 additions & 5 deletions packages/hooks/src/useLocalStorageState/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ const [state, setState] = useLocalStorageState<T>(
### Options
| 参数 | 说明 | 类型 | 默认值 |
| ------------ | ------------------ | ------------------------ | ---------------- |
| defaultValue | 默认值 | `any \| (() => any)` | - |
| serializer | 自定义序列化方法 | `(value: any) => string` | `JSON.stringify` |
| deserializer | 自定义反序列化方法 | `(value: string) => any` | `JSON.parse` |
| 参数 | 说明 | 类型 | 默认值 |
| ------------ | ------------------ | -------------------------- | ----------------------------- |
| defaultValue | 默认值 | `any \| (() => any)` | - |
| serializer | 自定义序列化方法 | `(value: any) => string` | `JSON.stringify` |
| deserializer | 自定义反序列化方法 | `(value: string) => any` | `JSON.parse` |
| onError | 错误回调函数 | `(error: unknown) => void` | `(e) => { console.error(e) }` |
## 备注
Expand Down

0 comments on commit d5fd82c

Please sign in to comment.