Skip to content

Export default SWR config to allow more flexible extensions #1023

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 5 commits into from
Mar 16, 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
7 changes: 6 additions & 1 deletion src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,12 @@ function useSWR<Data = any, Error = any>(
return memoizedState
}

const SWRConfig = SWRConfigContext.Provider
Object.defineProperty(SWRConfigContext.Provider, 'default', {
value: defaultConfig
})
const SWRConfig = SWRConfigContext.Provider as typeof SWRConfigContext.Provider & {
default: SWRConfiguration
}

export { trigger, mutate, SWRConfig }
export default useSWR
5 changes: 5 additions & 0 deletions test/use-swr-configs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { act, render, screen, fireEvent } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import useSWR, { mutate, SWRConfig } from '../src'
import { sleep } from './utils'
import defaultConfig from '../src/config'

describe('useSWR - configs', () => {
it('should read the config fallback from the context', async () => {
Expand Down Expand Up @@ -88,4 +89,8 @@ describe('useSWR - configs', () => {
await act(() => revalidate())
screen.getByText('data: 1')
})

it('should expose default config as static property on SWRConfig', () => {
expect(SWRConfig.default).toBe(defaultConfig)
})
})