diff --git a/src/types.ts b/src/types.ts index 37638f555..7120549b3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -144,7 +144,7 @@ export type MutatorCallback = ( export type MutatorOptions = { revalidate?: boolean - populateCache?: boolean | ((result: any, currentData: Data) => Data) + populateCache?: boolean | ((result: any, currentData?: Data) => Data) optimisticData?: Data | ((currentData?: Data) => Data) rollbackOnError?: boolean } @@ -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 diff --git a/src/utils/use-swr-config.ts b/src/utils/use-swr-config.ts index a12ec7b73..384f79602 100644 --- a/src/utils/use-swr-config.ts +++ b/src/utils/use-swr-config.ts @@ -5,7 +5,7 @@ import { mergeObjects } from './helper' import { FullConfiguration, Cache } from '../types' export const useSWRConfig = < - T extends Cache = Map + T extends Cache = Cache >(): FullConfiguration => { return mergeObjects(defaultConfig, useContext(SWRConfigContext)) } diff --git a/test/type/config.ts b/test/type/config.ts index 54a9ff9f7..cb556ce4f 100644 --- a/test/type/config.ts +++ b/test/type/config.ts @@ -6,7 +6,6 @@ interface CustomCache extends Cache { } export function useTestCache() { - expectType>(useSWRConfig().cache) - expectType>(useSWRConfig>().cache) + expectType(useSWRConfig().cache) expectType(useSWRConfig().cache) }