diff --git a/src/use-query/demo.tsx b/src/use-query/demo.tsx index ce235e7a..9031976b 100644 --- a/src/use-query/demo.tsx +++ b/src/use-query/demo.tsx @@ -181,6 +181,9 @@ function Demo5() { + @@ -189,6 +192,9 @@ function Demo5() { + diff --git a/src/use-query/index.tsx b/src/use-query/index.tsx index d673f554..4211f8fc 100644 --- a/src/use-query/index.tsx +++ b/src/use-query/index.tsx @@ -288,7 +288,8 @@ export function useQuery>, E = any> }) const refreshWithCache = useStableFn(async (params?: Parameters | []) => { - const actualParams = (params ?? service.params) || [] + const outerParams = cacheActions.isCacheEnabled() ? latest.current.cache.params : service.params + const actualParams = params ?? (outerParams || []) return service.refresh(actualParams) }) diff --git a/src/use-request/demo.tsx b/src/use-request/demo.tsx index d10a7e5d..ce7e322b 100644 --- a/src/use-request/demo.tsx +++ b/src/use-request/demo.tsx @@ -1,201 +1 @@ -import { Button, Card, KeyValue, OTP, Toaster, Zone, cn, toast, wait } from '@/components' -import { mutate, useCounter, useRequest } from '@shined/react-use' - -export function App() { - return ( - -

Immediate + Trigger by user + Dependencies

- -

Lifecycle + Refresh + Params + Mutate + Cancel

- -

Throttle + Debounce

- -

ReFocus + ReConnect + AutoRefresh + Loading Slow

- -

Error Retry + Cache (SWR)

- - -
- ) -} - -function Demo1() { - const [count, actions] = useCounter(0) - const { run, data, loading, error } = useRequest(wait, { refreshDependencies: [count] }) - return ( - <> - - - - - - - ) -} - -function Demo2() { - const { run, mutate, initializing, refresh, refreshing, params, cancel, data, loading } = useRequest( - (n = OTP()) => wait(300, n), - { - immediate: false, - cacheKey: 'cacheKeyForDemo5', - onBefore: (data, params) => console.log('onBefore', data, params), - onSuccess: (data, params) => console.log('onSuccess', data, params), - onFinally: (data, params) => console.log('onFinally', data, params), - onMutate: (data, params) => console.log('onMutate', data, params), - onCancel: (data, params) => console.log('onCancel', data, params), - onRefresh: (data, params) => console.log('onRefresh', data, params), - }, - ) - - return ( - <> - - - - - - - - - - - - - - ) -} - -function Demo3() { - const { run, data, loading, error } = useRequest(wait, { immediate: false, debounce: 300 }) - const { - run: run2, - data: data2, - loading: loading2, - error: error2, - } = useRequest(wait, { immediate: false, throttle: 1000 }) - return ( - <> - - - - - - - - - - ) -} - -function Demo4() { - const { data, initializing, loadingSlow } = useRequest((n = OTP()) => wait(1000, n), { - // refreshInterval: 6000, - refreshOnFocus: true, - refreshOnReconnect: true, - loadingTimeout: 500, - }) - - const slowStr = loadingSlow ? ' (slow...)' : '' - - return -} - -const localStorageProvider = { - get: (key: string) => { - const value = localStorage.getItem(key) - return value ? JSON.parse(value) : undefined - }, - set: (key: string, value: string) => localStorage.setItem(key, JSON.stringify(value)), - delete: (key: string) => localStorage.removeItem(key), - keys: () => Object.keys(localStorage)[Symbol.iterator](), -} - -let fetchCount = 0 - -function Demo5() { - const fetch = useRequest( - async (n = OTP()) => { - fetchCount += 1 - const res = await wait(1000, n) - if (fetchCount <= 1) throw new Error('Error!') - return res - }, - { - immediate: false, - compare() { - return true - }, - cacheKey: 'cacheKeyForDemo5', - // provider: localStorageProvider, - onErrorRetry: (error, { currentCount }) => toast.error(`Retry ${currentCount} failed.`), - }, - ) - - const fetch2 = useRequest((n = OTP()) => wait(1000, n), { - immediate: false, - cacheKey: 'cacheKeyForDemo5', - // provider: localStorageProvider, - }) - - const slowStr = fetch.loadingSlow ? ' (slow...)' : '' - - return ( - <> - - - - - - - - - - - - - - - - - - - ) -} +export { App } from '../use-query/demo'