From 03e23f927e1c9b1a86df10af14e3f626383eab87 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sat, 7 Aug 2021 19:15:40 +0800 Subject: [PATCH] BREAKING CHANGE: deprecate revalidate --- examples/axios-typescript/libs/useRequest.ts | 5 ++--- examples/focus-revalidate/pages/index.js | 6 +++--- examples/refetch-interval/pages/index.js | 6 +++--- infinite/index.ts | 7 ------- src/types.ts | 4 ---- src/use-swr.ts | 5 ----- test/use-swr-config-callbacks.test.tsx | 10 ++++----- test/use-swr-integration.test.tsx | 20 +++++++----------- test/use-swr-revalidate.test.tsx | 22 +++++++++++--------- 9 files changed, 33 insertions(+), 52 deletions(-) diff --git a/examples/axios-typescript/libs/useRequest.ts b/examples/axios-typescript/libs/useRequest.ts index 0a2651132..44ddb0495 100644 --- a/examples/axios-typescript/libs/useRequest.ts +++ b/examples/axios-typescript/libs/useRequest.ts @@ -6,7 +6,7 @@ export type GetRequest = AxiosRequestConfig | null interface Return extends Pick< SWRResponse, AxiosError>, - 'isValidating' | 'revalidate' | 'error' | 'mutate' + 'isValidating' | 'error' | 'mutate' > { data: Data | undefined response: AxiosResponse | undefined @@ -24,7 +24,7 @@ export default function useRequest( request: GetRequest, { initialData, ...config }: Config = {} ): Return { - const { data: response, error, isValidating, revalidate, mutate } = useSWR< + const { data: response, error, isValidating, mutate } = useSWR< AxiosResponse, AxiosError >( @@ -53,7 +53,6 @@ export default function useRequest( response, error, isValidating, - revalidate, mutate } } diff --git a/examples/focus-revalidate/pages/index.js b/examples/focus-revalidate/pages/index.js index 62751a9f7..345e77310 100644 --- a/examples/focus-revalidate/pages/index.js +++ b/examples/focus-revalidate/pages/index.js @@ -5,7 +5,7 @@ import { login, logout } from '../libs/auth' import useSWR from 'swr' export default () => { - const { data, revalidate } = useSWR('/api/user', fetch) + const { data, mutate } = useSWR('/api/user', fetch) if (!data) return

loading...

if (data.loggedIn) { @@ -14,7 +14,7 @@ export default () => { } else { @@ -22,7 +22,7 @@ export default () => {

Please login

} diff --git a/examples/refetch-interval/pages/index.js b/examples/refetch-interval/pages/index.js index 4560d10a9..97844698e 100644 --- a/examples/refetch-interval/pages/index.js +++ b/examples/refetch-interval/pages/index.js @@ -5,7 +5,7 @@ import fetch from '../libs/fetch' import useSWR from 'swr' export default () => { - const { data, revalidate } = useSWR('/api/data', fetch, { + const { data, mutate } = useSWR('/api/data', fetch, { // revalidate the data per second refreshInterval: 1000 }) @@ -21,7 +21,7 @@ export default () => { ev.preventDefault() setValue('') await fetch(`/api/data?add=${value}`) - revalidate() + mutate() }}> setValue(ev.target.value)} /> @@ -30,7 +30,7 @@ export default () => { ) diff --git a/infinite/index.ts b/infinite/index.ts index f48a6eece..fc00ac8b9 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -216,13 +216,6 @@ export const infinite = (((useSWRNext: SWRHook) => ( get: () => swr.data, enumerable: true }, - // revalidate will be deprecated in the 1.x release - // because mutate() covers the same use case of revalidate(). - // This remains only for backward compatibility - revalidate: { - get: () => swr.revalidate, - enumerable: true - }, isValidating: { get: () => swr.isValidating, enumerable: true diff --git a/src/types.ts b/src/types.ts index ac610cc59..d2e2a5dce 100644 --- a/src/types.ts +++ b/src/types.ts @@ -151,10 +151,6 @@ export type Key = ValueKey | (() => ValueKey) export interface SWRResponse { data?: Data error?: Error - /** - * @deprecated `revalidate` is deprecated, please use `mutate()` for the same purpose. - */ - revalidate: () => Promise mutate: KeyedMutator isValidating: boolean } diff --git a/src/use-swr.ts b/src/use-swr.ts index cd4046810..648b2ffef 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -598,13 +598,8 @@ export const useSWRHandler = ( throw isUndefined(error) ? revalidate({ dedupe: true }) : error } - // Define the SWR state. - // `revalidate` will be deprecated in the 1.x release - // because `mutate()` covers the same use case of `revalidate()`. - // This remains only for backward compatibility return Object.defineProperties( { - revalidate, mutate: boundMutate }, { diff --git a/test/use-swr-config-callbacks.test.tsx b/test/use-swr-config-callbacks.test.tsx index 26102ff7f..fdbca86c4 100644 --- a/test/use-swr-config-callbacks.test.tsx +++ b/test/use-swr-config-callbacks.test.tsx @@ -9,13 +9,13 @@ describe('useSWR - config callbacks', () => { let count = 0 function Page(props: { text: string }) { - const { data, revalidate } = useSWR( + const { data, mutate } = useSWR( 'config callbacks - onSuccess', () => createResponse(count++), { onSuccess: () => (state = props.text) } ) return ( -
+
mutate()}> hello, {data}, {props.text}
) @@ -46,19 +46,19 @@ describe('useSWR - config callbacks', () => { let count = 0 function Page(props: { text: string }) { - const { data, revalidate, error } = useSWR( + const { data, mutate, error } = useSWR( 'config callbacks - onError', () => createResponse(new Error(`Error: ${count++}`)), { onError: () => (state = props.text) } ) if (error) return ( -
+
mutate()}> {error.message}
) return ( -
+
mutate()}> hello, {data}, {props.text}
) diff --git a/test/use-swr-integration.test.tsx b/test/use-swr-integration.test.tsx index c4d318610..c6816e679 100644 --- a/test/use-swr-integration.test.tsx +++ b/test/use-swr-integration.test.tsx @@ -187,22 +187,18 @@ describe('useSWR', () => { it('should broadcast isValidating', async () => { function useBroadcast3() { - const { isValidating, revalidate } = useSWR( - 'broadcast-3', - () => sleep(100), - { - // need to turn of deduping otherwise - // revalidating will be ignored - dedupingInterval: 10 - } - ) - return { isValidating, revalidate } + const { isValidating, mutate } = useSWR('broadcast-3', () => sleep(100), { + // need to turn of deduping otherwise + // revalidating will be ignored + dedupingInterval: 10 + }) + return { isValidating, mutate } } function Initiator() { - const { isValidating, revalidate } = useBroadcast3() + const { isValidating, mutate } = useBroadcast3() useEffect(() => { const timeout = setTimeout(() => { - revalidate() + mutate() }, 200) return () => clearTimeout(timeout) // the revalidate function is always the same reference because the key of the useSWR is static (broadcast-3) diff --git a/test/use-swr-revalidate.test.tsx b/test/use-swr-revalidate.test.tsx index 00539ebf9..18b1a9226 100644 --- a/test/use-swr-revalidate.test.tsx +++ b/test/use-swr-revalidate.test.tsx @@ -8,8 +8,8 @@ describe('useSWR - revalidate', () => { let value = 0 function Page() { - const { data, revalidate } = useSWR('dynamic-3', () => value++) - return + const { data, mutate } = useSWR('dynamic-3', () => value++) + return } render() @@ -29,10 +29,10 @@ describe('useSWR - revalidate', () => { let value = 0 function Page() { - const { data: v1, revalidate } = useSWR('dynamic-4', () => value++) + const { data: v1, mutate } = useSWR('dynamic-4', () => value++) const { data: v2 } = useSWR('dynamic-4', () => value++) return ( - ) @@ -56,11 +56,11 @@ describe('useSWR - revalidate', () => { let faster = false function Page() { - const { data, revalidate } = useSWR('race', () => + const { data, mutate } = useSWR('race', () => createResponse(faster ? 1 : 0, { delay: faster ? 50 : 100 }) ) - return + return } render() @@ -83,14 +83,16 @@ describe('useSWR - revalidate', () => { it('should keep isValidating be true when there are two concurrent requests', async () => { function Page() { - const { isValidating, revalidate } = useSWR( + const { isValidating, mutate } = useSWR( 'keep isValidating for concurrent requests', () => createResponse(null, { delay: 100 }), { revalidateOnMount: false } ) return ( - + ) } @@ -114,7 +116,7 @@ describe('useSWR - revalidate', () => { it('should respect sequences of revalidation calls although in dedupingInterval', async () => { let count = 0 function Page() { - const { data, revalidate } = useSWR( + const { data, mutate } = useSWR( 'respect sequences of revalidation calls although in dedupingInterval', () => { const currCount = ++count @@ -124,7 +126,7 @@ describe('useSWR - revalidate', () => { dedupingInterval: 30 } ) - return
revalidate()}>count: {data}
+ return
mutate()}>count: {data}
} render()