Skip to content
Open
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: 19 additions & 1 deletion packages/hooks/src/useSetState/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, test } from 'vitest';
import useSetState from '../index';

describe('useSetState', () => {
const setUp = <T extends object>(initialValue: T) =>
const setUp = <T extends object>(initialValue?: T) =>
renderHook(() => {
const [state, setState] = useSetState<T>(initialValue);
return {
Expand Down Expand Up @@ -38,4 +38,22 @@ describe('useSetState', () => {
});
expect(hook.result.current.state).toEqual({ count: 1 });
});

test('should support empty initial state', () => {
const hook = renderHook(() => {
const [state, setState] = useSetState<{ hello?: string }>();
return {
state,
setState,
} as const;
});

expect(hook.result.current.state).toEqual({});

act(() => {
hook.result.current.setState({ hello: 'world' });
});

expect(hook.result.current.state).toEqual({ hello: 'world' });
});
});
4 changes: 2 additions & 2 deletions packages/hooks/src/useSetState/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ useSetState works similar to `this.setState` of class component, used to manage
## API

```typescript
const [state, setState] = useSetState<T>(initialState);
const [state, setState] = useSetState<T>(initialState?);
```

### Result
Expand All @@ -34,4 +34,4 @@ const [state, setState] = useSetState<T>(initialState);

| Property | Description | Type | Default |
| ------------ | ------------- | -------------- | ------- |
| initialState | Initial state | `T \| () => T` | - |
| initialState | Initial state | `T \| () => T` | `{}` |
2 changes: 1 addition & 1 deletion packages/hooks/src/useSetState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SetState<S extends Record<string, any>> = <K extends keyof S>(
) => void;

const useSetState = <S extends Record<string, any>>(
initialState: S | (() => S),
initialState: S | (() => S) = {} as S,
): [S, SetState<S>] => {
const [state, setState] = useState<S>(initialState);

Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useSetState/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nav:
## API

```typescript
const [state, setState] = useSetState<T>(initialState);
const [state, setState] = useSetState<T>(initialState?);
```

### Result
Expand All @@ -34,4 +34,4 @@ const [state, setState] = useSetState<T>(initialState);

| 参数 | 说明 | 类型 | 默认值 |
| ------------ | -------- | -------------- | ------ |
| initialState | 初始状态 | `T \| () => T` | - |
| initialState | 初始状态 | `T \| () => T` | `{}` |
Loading