Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(createUseStorageState): adjust type of defaultValue and update docs #2148

Merged
merged 8 commits into from
Jul 12, 2023
18 changes: 6 additions & 12 deletions packages/hooks/src/createUseStorageState/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/* eslint-disable no-empty */
import { useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';
import useUpdateEffect from '../useUpdateEffect';
import { isFunction, isUndef } from '../utils';

export interface IFuncUpdater<T> {
(previousState?: T): T;
}
export interface IFuncStorage {
(): Storage;
}
export type SetState<S> = S | ((prevState?: S) => S);

export interface Options<T> {
defaultValue?: T | (() => T);
serializer?: (value: T) => string;
deserializer?: (value: string) => T;
defaultValue?: T | IFuncUpdater<T>;
onError?: (error: unknown) => void;
}

Expand All @@ -34,14 +28,14 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
onError(err);
}

const serializer = (value: T) => {
const serializer = (value: T): string => {
if (options?.serializer) {
return options?.serializer(value);
}
return JSON.stringify(value);
};

const deserializer = (value: string) => {
const deserializer = (value: string): T => {
if (options?.deserializer) {
return options?.deserializer(value);
}
Expand All @@ -63,13 +57,13 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
return options?.defaultValue;
}

const [state, setState] = useState<T>(() => getStoredValue());
const [state, setState] = useState<T | undefined>(() => getStoredValue());

useUpdateEffect(() => {
setState(getStoredValue());
}, [key]);

const updateState = (value: T | IFuncUpdater<T>) => {
const updateState = (value?: SetState<T>) => {
const currentState = isFunction(value) ? value(state) : value;
setState(currentState);

Expand Down
12 changes: 11 additions & 1 deletion packages/hooks/src/useLocalStorageState/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ A Hook that store state into localStorage.
If you want to delete this record from localStorage, you can use `setState()` or `setState(undefined)`.

```typescript
type SetState<S> = S | ((prevState?: S) => S);

interface Options<T> {
defaultValue?: T | (() => T);
serializer?: (value: T) => string;
deserializer?: (value: string) => T;
onError?: (error: unknown) => void;
}

const [state, setState] = useLocalStorageState<T>(
key: string,
options: Options<T>
): [T?, (value?: T | ((previousState: T) => T)) => void];
): [T?, (value?: SetState<T>) => void];
```

### Result

| Property | Description | Type |
| -------- | --------------------------- | ------------------------------- |
| state | Local `localStorage` value | `T` |
| setState | Update `localStorage` value | `(value?: SetState<T>) => void` |

### Options

| Property | Description | Type | Default |
Expand Down
12 changes: 11 additions & 1 deletion packages/hooks/src/useLocalStorageState/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ nav:
如果想从 localStorage 中删除这条数据,可以使用 `setState()` 或 `setState(undefined)` 。

```typescript
type SetState<S> = S | ((prevState?: S) => S);

interface Options<T> {
defaultValue?: T | (() => T);
serializer?: (value: T) => string;
deserializer?: (value: string) => T;
onError?: (error: unknown) => void;
}

const [state, setState] = useLocalStorageState<T>(
key: string,
options: Options<T>
): [T?, (value?: T | ((previousState: T) => T)) => void];
): [T?, (value?: SetState<T>) => void];
```

### Result

| 参数 | 说明 | 类型 |
| -------- | ---------------------- | ------------------------------- |
| state | 本地 `localStorage` 值 | `T` |
| setState | 设置 `localStorage` 值 | `(value?: SetState<T>) => void` |

### Options

| 参数 | 说明 | 类型 | 默认值 |
Expand Down