Skip to content

Commit

Permalink
Export default SWR config to allow more flexible extensions (#1023)
Browse files Browse the repository at this point in the history
* Export default SWR config to allow more flexible extensions

* export default config as static property on SWRConfig
  • Loading branch information
jakubriedl authored Mar 16, 2021
1 parent bbfc7d7 commit 7f490c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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)
})
})

0 comments on commit 7f490c1

Please sign in to comment.