Skip to content

Commit f9e509d

Browse files
authored
fix: stale params when revalidate (#342)
1 parent 49b9b37 commit f9e509d

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/useList/__tests__/useList.test.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { renderHook } from '@testing-library/react-hooks';
33
import useList from '..';
44

55
describe('Test useList hook', () => {
6-
it('Should get initial data with default params', () => {
6+
it('Should get initial data with default params', async () => {
77
const fetcher = jest.fn().mockResolvedValue({
88
total: 1,
99
data: [{ uuid: 1 }],
@@ -12,12 +12,12 @@ describe('Test useList hook', () => {
1212
const { result } = renderHook(() => useList(fetcher, { current: 1, pageSize: 20 }));
1313

1414
expect(fetcher).toBeCalledTimes(1);
15-
waitFor(() => {
15+
await waitFor(() => {
1616
expect(result.current.data.length).toBe(1);
17-
expect(result.current.params).toBe(
18-
expect.objectContaining({ current: 1, pageSize: 20, total: 1 })
19-
);
2017
});
18+
expect(result.current.params).toEqual(
19+
expect.objectContaining({ current: 1, pageSize: 20, total: 1 })
20+
);
2121
});
2222

2323
it('Should get data after mutating', () => {
@@ -35,10 +35,9 @@ describe('Test useList hook', () => {
3535
result.current.mutate({ search: 'test' });
3636
});
3737

38-
waitFor(() => {
39-
expect(fetcher).toBeCalledTimes(2);
40-
expect(result.current.params).toBe(expect.objectContaining({ search: 'test' }));
41-
});
38+
expect(fetcher).toBeCalledTimes(2);
39+
expect(result.current.params).toEqual(expect.objectContaining({ search: 'test' }));
40+
expect(fetcher.mock.calls[1][0]).toEqual(expect.objectContaining({ search: 'test' }));
4241
});
4342

4443
it('Should support revalidate after mutating', () => {
@@ -56,9 +55,7 @@ describe('Test useList hook', () => {
5655
result.current.mutate({ search: 'test' }, { revalidate: false });
5756
});
5857

59-
waitFor(() => {
60-
expect(result.current.params).toBe(expect.objectContaining({ search: 'test' }));
61-
});
58+
expect(result.current.params).toEqual(expect.objectContaining({ search: 'test' }));
6259
});
6360

6461
it('Should support get data with current params', () => {
@@ -76,8 +73,6 @@ describe('Test useList hook', () => {
7673
result.current.mutate();
7774
});
7875

79-
waitFor(() => {
80-
expect(fetcher).toBeCalledTimes(2);
81-
});
76+
expect(fetcher).toBeCalledTimes(2);
8277
});
8378
});

src/useList/index.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export default function useList<T extends Record<string, any>, P extends Record<
3030

3131
const options = useMemo(() => merge(rawOptions, { immediate: true }), [rawOptions]);
3232

33-
const performFetch = () => {
33+
const performFetch = (raw = params) => {
3434
setLoading(true);
35-
fetcher(params)
35+
fetcher(raw)
3636
.then(({ data, total }) => {
3737
setData(data);
3838
setTotal(total);
@@ -44,15 +44,28 @@ export default function useList<T extends Record<string, any>, P extends Record<
4444
};
4545

4646
const mutate = (next: Partial<P> | ((prev: P) => P) = params, options: IMutateOptions = {}) => {
47-
setParams(typeof next === 'function' ? next : { ...merge(params, next) });
48-
4947
const defaultOptions: IMutateOptions = {
5048
revalidate: true,
5149
};
52-
5350
const nextOptions = merge(defaultOptions, options);
54-
if (nextOptions.revalidate) {
55-
performFetch();
51+
52+
if (typeof next === 'function') {
53+
setParams((prev) => {
54+
const tmp = next(prev);
55+
56+
if (nextOptions.revalidate) {
57+
performFetch(tmp);
58+
}
59+
60+
return tmp;
61+
});
62+
} else {
63+
const tmp = { ...merge({}, params, next) };
64+
setParams(tmp);
65+
66+
if (nextOptions.revalidate) {
67+
performFetch(tmp);
68+
}
5669
}
5770
};
5871

0 commit comments

Comments
 (0)