Skip to content

feat: Use the latest reference of fetcher #1727

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

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const WITH_DEDUPE = { dedupe: true }

export const useSWRHandler = <Data = any, Error = any>(
_key: Key,
fn: Fetcher<Data> | null,
fetcher: Fetcher<Data> | null,
config: typeof defaultConfig & SWRConfiguration<Data, Error>
) => {
const {
Expand Down Expand Up @@ -74,6 +74,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// Refs to keep the key and config.
const keyRef = useRef(key)
const fetcherRef = useRef(fetcher)
const configRef = useRef(config)
const getConfig = () => configRef.current

Expand Down Expand Up @@ -106,7 +107,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// Resolve the current validating state.
const resolveValidating = () => {
if (!key || !fn) return false
if (!key || !fetcher) return false
if (cache.get(keyValidating)) return true

// If it's not mounted yet and it should revalidate on mount, revalidate.
Expand All @@ -127,7 +128,14 @@ export const useSWRHandler = <Data = any, Error = any>(
// `fetcher`, to correctly handle the many edge cases.
const revalidate = useCallback(
async (revalidateOpts?: RevalidatorOptions): Promise<boolean> => {
if (!key || !fn || unmountedRef.current || getConfig().isPaused()) {
const currentFetcher = fetcherRef.current

if (
!key ||
!currentFetcher ||
unmountedRef.current ||
getConfig().isPaused()
) {
return false
}

Expand Down Expand Up @@ -196,7 +204,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// Start the request and keep the timestamp.
CONCURRENT_PROMISES_TS[key] = getTimestamp()
CONCURRENT_PROMISES[key] = fn(...fnArgs)
CONCURRENT_PROMISES[key] = currentFetcher(...fnArgs)
}

// Wait until the ongoing request is done. Deduplication is also
Expand Down Expand Up @@ -345,8 +353,9 @@ export const useSWRHandler = <Data = any, Error = any>(
[]
)

// Always update config.
// Always update fetcher and config refs.
useIsomorphicLayoutEffect(() => {
fetcherRef.current = fetcher
configRef.current = config
})

Expand Down
36 changes: 36 additions & 0 deletions test/use-swr-fetcher.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { act, screen } from '@testing-library/react'
import React, { useState } from 'react'
import useSWR from 'swr'
import { createKey, renderWithConfig, nextTick } from './utils'

describe('useSWR - fetcher', () => {
// https://github.com/vercel/swr/issues/1131
it('should use the latest fetcher reference', async () => {
const key = createKey()
let fetcher = () => 'foo'
let mutate
let rerender

function Page() {
const { data, mutate: boundMutate } = useSWR(key, fetcher)
rerender = useState({})[1]
mutate = boundMutate

return <div>data:{data}</div>
}

renderWithConfig(<Page />)
await nextTick()
screen.getByText('data:foo')

// Change the fetcher and make sure the ref is updated.
fetcher = () => 'bar'
act(() => rerender({}))

// Revalidate.
await act(() => mutate())

// Should fetch with the new fetcher.
await screen.findByText('data:bar')
})
})