Skip to content

perf: optimize useSWRConfig with useMemo to maintain stable reference #4110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions src/_internal/utils/use-swr-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { useContext } from 'react'
import { useContext, useMemo } from 'react'
import { defaultConfig } from './config'
import { SWRConfigContext } from './config-context'
import { mergeObjects } from './shared'
import type { FullConfiguration } from '../types'

export const useSWRConfig = (): FullConfiguration => {
return mergeObjects(defaultConfig, useContext(SWRConfigContext))
const parentConfig = useContext(SWRConfigContext)
const mergedConfig = useMemo(
() => mergeObjects(defaultConfig, parentConfig),
[parentConfig]
)
return mergedConfig
}
53 changes: 51 additions & 2 deletions test/use-swr-context-config.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { act, screen } from '@testing-library/react'
import useSWR, { mutate } from 'swr'
import { act, render, screen } from '@testing-library/react'
import useSWR, {
mutate,
SWRConfig,
type SWRConfiguration,
useSWRConfig
} from 'swr'
import { createKey, createResponse, renderWithGlobalCache } from './utils'
import { useCallback, useEffect, useState } from 'react'

describe('useSWR - context configs', () => {
it('mutate before mount should not block rerender', async () => {
Expand All @@ -25,3 +31,46 @@ describe('useSWR - context configs', () => {
await screen.findByText('data')
})
})

describe('useSWRConfig hook maintains stable reference across re-renders', () => {
it('should maintain the same swrConfig reference when counter updates', () => {
const parentConfig: SWRConfiguration = {
revalidateOnMount: true,
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false
}
const counterButtonText = 'counter + 1'
let useSWRConfigReferenceChangedTimes = 0
function Page() {
return (
<SWRConfig value={parentConfig}>
<ChildComponent />
</SWRConfig>
)
}
function ChildComponent() {
const swrConfig = useSWRConfig()
const [, setCounter] = useState(0)
const counterAddOne = useCallback(
() => setCounter(prev => prev + 1),
[setCounter]
)
useEffect(() => {
useSWRConfigReferenceChangedTimes += 1
}, [swrConfig])
return <button onClick={counterAddOne}>{counterButtonText}</button>
}
render(<Page />)
act(() => {
screen.getByText(counterButtonText).click()
})
act(() => {
screen.getByText(counterButtonText).click()
})
act(() => {
screen.getByText(counterButtonText).click()
})
expect(useSWRConfigReferenceChangedTimes).toBe(1)
})
})
Loading