diff --git a/src/types.ts b/src/types.ts index 37638f555..6750d90fa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -255,14 +255,12 @@ export type RevalidateCallback = ( type: K ) => RevalidateCallbackReturnType[K] -export type StateUpdateCallback = (state: { - data?: Data - error?: Error - isValidating?: boolean -}) => void +export type StateUpdateCallback = ( + state: State +) => void -export interface Cache { - get(key: Key): Data | null | undefined - set(key: Key, value: Data): void +export interface Cache { + get(key: Key): State | undefined + set(key: Key, value: State): void delete(key: Key): void } diff --git a/src/use-swr.ts b/src/use-swr.ts index c2a2f3ca1..d618e684b 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -178,7 +178,7 @@ export const useSWRHandler = ( // new request should be initiated. const shouldStartNewRequest = !FETCH[key] || !opts.dedupe - /* + /* For React 17 Do unmount check for calls: If key has changed during the revalidation, or the component has been @@ -442,7 +442,8 @@ export const useSWRHandler = ( mergeObjects( { error: state.error, - isValidating: state.isValidating + isValidating: state.isValidating, + isLoading: state.isLoading }, // Since `setState` only shallowly compares states, we do a deep // comparison here. diff --git a/src/utils/cache.ts b/src/utils/cache.ts index b45ab5e5c..81cf82282 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -8,6 +8,7 @@ import * as revalidateEvents from '../constants' import { Key, Cache, + State, ScopedMutator, RevalidateEvent, RevalidateCallback, @@ -106,13 +107,9 @@ export const createCacheHelper = ( ) => [ // Getter - () => cache.get(key) || {}, + () => (cache.get(key) || {}) as State & Partial, // Setter - ( - info: Partial< - { data: Data; error: any; isValidating: boolean } | ExtendedInfo - > - ) => { + (info: Partial | ExtendedInfo>) => { cache.set(key, mergeObjects(cache.get(key), info)) } ] as const