Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding initialData #106

Merged
merged 6 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface ConfigInterface<Data = any, Error = any> {
export type fetcherFn<Data> = (...args: any) => Data | Promise<Data>
export interface ConfigInterface<
Data = any,
Error = any,
Fn extends fetcherFn<Data> = fetcherFn<Data>
> {
errorRetryInterval?: number
loadingTimeout?: number
focusThrottleInterval?: number
Expand All @@ -8,8 +13,9 @@ export interface ConfigInterface<Data = any, Error = any> {
refreshWhenHidden?: boolean
revalidateOnFocus?: boolean
shouldRetryOnError?: boolean
fetcher?: any
fetcher?: Fn
suspense?: boolean
initialData?: Data

onLoadingSlow?: (key: string, config: ConfigInterface<Data, Error>) => void
onSuccess?: (
Expand Down
2 changes: 1 addition & 1 deletion src/use-swr-pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function App () {
}
*/

export function useSWRPages<OffsetType, Data, Error>(
export function useSWRPages<OffsetType = any, Data = any, Error = any>(
pageKey: string,
pageFn: pageComponentType<OffsetType, Data, Error>,
SWRToOffset: pageOffsetMapperType<OffsetType, Data, Error>,
Expand Down
9 changes: 5 additions & 4 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
triggerInterface,
mutateInterface,
broadcastStateInterface,
responseInterface
responseInterface,
fetcherFn
} from './types'

import defaultConfig, {
Expand Down Expand Up @@ -111,14 +112,14 @@ function useSWR<Data = any, Error = any>(
): responseInterface<Data, Error>
function useSWR<Data = any, Error = any>(
key: keyInterface,
fn?: Function,
fn?: fetcherFn<Data>,
config?: ConfigInterface<Data, Error>
): responseInterface<Data, Error>
function useSWR<Data = any, Error = any>(
...args
): responseInterface<Data, Error> {
let _key: keyInterface,
fn: Function | undefined,
fn: fetcherFn<Data> | undefined,
config: ConfigInterface<Data, Error> = {}
if (args.length >= 1) {
_key = args[0]
Expand Down Expand Up @@ -157,7 +158,7 @@ function useSWR<Data = any, Error = any>(
const shouldReadCache = config.suspense || !useHydration()

// stale: get from cache
let [data, setData] = useState(shouldReadCache ? cacheGet(key) : undefined)
let [data, setData] = useState(config.initialData || (shouldReadCache ? cacheGet(key) : undefined))
let [error, setError] = useState(
shouldReadCache ? cacheGet(keyErr) : undefined
)
Expand Down