Skip to content

Commit 7f490c1

Browse files
authored
Export default SWR config to allow more flexible extensions (#1023)
* Export default SWR config to allow more flexible extensions * export default config as static property on SWRConfig
1 parent bbfc7d7 commit 7f490c1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/use-swr.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,12 @@ function useSWR<Data = any, Error = any>(
804804
return memoizedState
805805
}
806806

807-
const SWRConfig = SWRConfigContext.Provider
807+
Object.defineProperty(SWRConfigContext.Provider, 'default', {
808+
value: defaultConfig
809+
})
810+
const SWRConfig = SWRConfigContext.Provider as typeof SWRConfigContext.Provider & {
811+
default: SWRConfiguration
812+
}
808813

809814
export { trigger, mutate, SWRConfig }
810815
export default useSWR

test/use-swr-configs.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
33
import useSWR, { mutate, SWRConfig } from '../src'
44
import { sleep } from './utils'
5+
import defaultConfig from '../src/config'
56

67
describe('useSWR - configs', () => {
78
it('should read the config fallback from the context', async () => {
@@ -88,4 +89,8 @@ describe('useSWR - configs', () => {
8889
await act(() => revalidate())
8990
screen.getByText('data: 1')
9091
})
92+
93+
it('should expose default config as static property on SWRConfig', () => {
94+
expect(SWRConfig.default).toBe(defaultConfig)
95+
})
9196
})

0 commit comments

Comments
 (0)