Skip to content

Commit 80b778f

Browse files
authored
Merge branch 'master' into master
2 parents 286932c + d02d146 commit 80b778f

File tree

5 files changed

+82
-28
lines changed

5 files changed

+82
-28
lines changed

docs/guide/upgrade.en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ useRequest has been rewritten:
3636
- Removed `pagination` related options, it is recommended to use `usePagination` or `useAntdTable` to achieve paging ability.
3737
- Removed `loadMore` related options, it is recommended to use `useInfiniteScroll` to achieve unlimited loading ability.
3838
- Removed `fetchKey`, that is, deleted concurrent request.
39-
- Removed `formatResult`, `initialData`, and `thrownError`.
39+
- Removed `formatResult`, `initialData`, and `throwOnError`.
4040
- The request library is no longer integrated by default, and `service` no longer supports string or object.
4141
- Added `runAsync` and `refreshAsync`, the original `run` no longer returns Promise.
4242
- Added error retry ability.

docs/guide/upgrade.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ useRequest 完全进行了重写:
3636
- 删除了 `pagination` 相关属性,建议使用 `usePagination``useAntdTable` 来实现分页能力。
3737
- 删除了 `loadMore` 相关属性,建议使用 `useInfiniteScroll` 来实现无限加载能力。
3838
- 删除了 `fetchKey`,也就是删除了并行能力。
39-
- 删除了 `formatResult``initialData``thrownError`
39+
- 删除了 `formatResult``initialData``throwOnError`
4040
- 不再默认集成请求库,`service` 不再支持字符或对象。
4141
- 新增了 `runAsync``refreshAsync`,原来的 `run` 不再返回 Promise。
4242
- 新增了错误重试能力。

packages/hooks/src/useResetState/__tests__/index.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { act, renderHook } from '@testing-library/react';
22
import useResetState from '../index';
33

44
describe('useResetState', () => {
5-
const setUp = <S>(initialState: S) =>
5+
const setUp = (initialState) =>
66
renderHook(() => {
7-
const [state, setState, resetState] = useResetState<S>(initialState);
7+
const [state, setState, resetState] = useResetState(initialState);
88

99
return {
1010
state,
@@ -20,6 +20,13 @@ describe('useResetState', () => {
2020
expect(hook.result.current.state).toEqual({ hello: 'world' });
2121
});
2222

23+
it('should support functional initialValue', () => {
24+
const hook = setUp(() => ({
25+
hello: 'world',
26+
}));
27+
expect(hook.result.current.state).toEqual({ hello: 'world' });
28+
});
29+
2330
it('should reset state', () => {
2431
const hook = setUp({
2532
hello: '',
@@ -49,4 +56,38 @@ describe('useResetState', () => {
4956
});
5057
expect(hook.result.current.state).toEqual({ count: 1 });
5158
});
59+
60+
it('should keep random initial state', () => {
61+
const random = Math.random();
62+
const hook = setUp({
63+
count: random,
64+
});
65+
66+
act(() => {
67+
hook.result.current.setState({ count: Math.random() });
68+
});
69+
70+
act(() => {
71+
hook.result.current.resetState();
72+
});
73+
74+
expect(hook.result.current.state).toEqual({ count: random });
75+
});
76+
77+
it('should support random functional initialValue', () => {
78+
const random = Math.random();
79+
const hook = setUp(() => ({
80+
count: random,
81+
}));
82+
83+
act(() => {
84+
hook.result.current.setState({ count: Math.random() });
85+
});
86+
87+
act(() => {
88+
hook.result.current.resetState();
89+
});
90+
91+
expect(hook.result.current.state).toEqual({ count: random });
92+
});
5293
});
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
2+
import { Button, Space } from 'antd';
23
import { useResetState } from 'ahooks';
34

4-
interface State {
5-
hello: string;
6-
count: number;
7-
}
8-
95
export default () => {
10-
const [state, setState, resetState] = useResetState<State>({
6+
const initialValue = {
117
hello: '',
12-
count: 0,
13-
});
8+
value: Math.random(),
9+
};
10+
const initialValueMemo = useMemo(() => {
11+
return initialValue;
12+
}, []);
13+
14+
const [state, setState, resetState] = useResetState(initialValue);
1415

1516
return (
1617
<div>
18+
<div>initial state: </div>
19+
<pre>{JSON.stringify(initialValueMemo, null, 2)}</pre>
20+
<div>current state: </div>
1721
<pre>{JSON.stringify(state, null, 2)}</pre>
18-
<p>
19-
<button
20-
type="button"
21-
style={{ marginRight: '8px' }}
22-
onClick={() => setState({ hello: 'world', count: 1 })}
22+
<Space>
23+
<Button
24+
onClick={() =>
25+
setState(() => ({
26+
hello: 'world',
27+
value: Math.random(),
28+
}))
29+
}
2330
>
24-
set hello and count
25-
</button>
26-
27-
<button type="button" onClick={resetState}>
28-
resetState
29-
</button>
30-
</p>
31+
set hello and value
32+
</Button>
33+
<Button onClick={resetState}>resetState</Button>
34+
</Space>
3135
</div>
3236
);
3337
};

packages/hooks/src/useResetState/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { useState } from 'react';
1+
import { useRef, useState } from 'react';
22
import type { Dispatch, SetStateAction } from 'react';
3+
import { isFunction } from '../utils';
34
import useMemoizedFn from '../useMemoizedFn';
5+
import useCreation from '../useCreation';
46

57
type ResetState = () => void;
68

79
const useResetState = <S>(
810
initialState: S | (() => S),
911
): [S, Dispatch<SetStateAction<S>>, ResetState] => {
10-
const [state, setState] = useState(initialState);
12+
const initialStateRef = useRef(initialState);
13+
const initialStateMemo = useCreation(
14+
() =>
15+
isFunction(initialStateRef.current) ? initialStateRef.current() : initialStateRef.current,
16+
[],
17+
);
18+
19+
const [state, setState] = useState(initialStateMemo);
1120

1221
const resetState = useMemoizedFn(() => {
13-
setState(initialState);
22+
setState(initialStateMemo);
1423
});
1524

1625
return [state, setState, resetState];

0 commit comments

Comments
 (0)