diff --git a/README.md b/README.md index 5a17ddc73..32fae0aa2 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options) #### Options - `suspense = false`: enable React Suspense mode [(details)](#suspense-mode) -- `fetcher = window.fetch`: the default fetcher function +- `fetcher`: the function to retrieve the remote data source - `initialData`: initial data to be returned (note: This is per-hook) - `revalidateOnMount`: enable or disable automatic revalidation when component is mounted (by default revalidation occurs on mount when initialData is not set, use this flag to force behavior) - `revalidateOnFocus = true`: auto revalidate when window gets focused diff --git a/src/types.ts b/src/types.ts index d73fd1c39..31c5fdc89 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,7 +18,7 @@ export interface Configuration< shouldRetryOnError: boolean suspense?: boolean initialData?: Data - fetcher: Fn + fetcher?: Fn cache: Cache middlewares?: Middleware[] diff --git a/src/utils/config.ts b/src/utils/config.ts index 1a30c5445..4f4e44ee7 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -6,7 +6,6 @@ import { slowConnection } from './env' import { Configuration, RevalidatorOptions, Revalidator } from '../types' import { UNDEFINED } from './helper' -const fetcher = (url: string) => fetch(url).then(res => res.json()) const noop = () => {} // error retry @@ -56,7 +55,6 @@ const defaultConfig: Configuration = { loadingTimeout: slowConnection ? 5000 : 3000, // providers - fetcher, compare: dequal, isPaused: () => false, cache: wrapCache(new Map()), diff --git a/test/use-swr-integration.test.tsx b/test/use-swr-integration.test.tsx index 585980d4e..c4d318610 100644 --- a/test/use-swr-integration.test.tsx +++ b/test/use-swr-integration.test.tsx @@ -357,27 +357,4 @@ describe('useSWR', () => { expect(fetcher).toBeCalled() await screen.findByText('hello, SWR') }) - - it('should use fetch api as default fetcher', async () => { - const users = [{ name: 'bob' }, { name: 'sue' }] - global['fetch'] = () => Promise.resolve() - const mockFetch = body => - Promise.resolve({ json: () => Promise.resolve(body) } as any) - const fn = jest - .spyOn(window, 'fetch') - .mockImplementation(() => mockFetch(users)) - - function Users() { - const { data } = useSWR('http://localhost:3000/api/users') - - return
hello, {data && data.map(u => u.name).join(' and ')}
- } - - render() - screen.getByText('hello,') - expect(fn).toBeCalled() - - await screen.findByText('hello, bob and sue') - delete global['fetch'] - }) }) diff --git a/test/use-swr-local-mutation.test.tsx b/test/use-swr-local-mutation.test.tsx index cec53f775..db0c70c1d 100644 --- a/test/use-swr-local-mutation.test.tsx +++ b/test/use-swr-local-mutation.test.tsx @@ -2,7 +2,7 @@ import { act, render, screen, fireEvent } from '@testing-library/react' import React, { useEffect, useState } from 'react' import useSWR, { mutate, createCache, SWRConfig } from 'swr' import { serialize } from '../src/utils/serialize' -import { createResponse, sleep, nextTick as waitForNextTick } from './utils' +import { createResponse, sleep, nextTick } from './utils' describe('useSWR - local mutation', () => { it('should trigger revalidation programmatically', async () => { @@ -29,6 +29,33 @@ describe('useSWR - local mutation', () => { await screen.findByText('data: 1') }) + it('should share local state when no fetcher is specified', async () => { + const useSharedState = (key, initialData) => { + const { data: state, mutate: setState } = useSWR(key, { initialData }) + return [state, setState] + } + + function Page() { + const [name, setName] = useSharedState('name', 'huozhi') + const [job, setJob] = useSharedState('job', 'gardener') + + return ( + { + setName('@huozhi') + setJob('chef') + }} + > + {name}:{job} + + ) + } + render() + const root = screen.getByText('huozhi:gardener') + fireEvent.click(root) + await screen.findByText('@huozhi:chef') + }) + it('should trigger revalidation programmatically within a dedupingInterval', async () => { let value = 0 @@ -144,7 +171,7 @@ describe('useSWR - local mutation', () => { //mount await screen.findByText('data: 0') - await waitForNextTick() + await nextTick() await act(() => { // mutate and revalidate return mutate('mutate-promise', createResponse(999), false) @@ -167,7 +194,7 @@ describe('useSWR - local mutation', () => { //mount await screen.findByText('data: 0') - await waitForNextTick() + await nextTick() await act(() => { // mutate and revalidate return mutate('mutate-async-fn', async () => createResponse(999), false)