Skip to content
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

fix: Return undefined data if key's falsy under suspense mode #1698

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
2 changes: 1 addition & 1 deletion src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// If there is `error`, the `error` needs to be thrown to the error boundary.
// If there is no `error`, the `revalidation` promise needs to be thrown to
// the suspense boundary.
if (suspense && isUndefined(data)) {
if (suspense && isUndefined(data) && key) {
throw isUndefined(error) ? revalidate(WITH_DEDUPE) : error
}

Expand Down
10 changes: 2 additions & 8 deletions test/use-swr-config.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { act, screen, fireEvent } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import useSWR, { SWRConfig, useSWRConfig, Middleware } from 'swr'
import {
sleep,
renderWithConfig,
createKey,
renderWithGlobalCache
} from './utils'
import { renderWithConfig, createKey, renderWithGlobalCache } from './utils'

describe('useSWR - configs', () => {
it('should read the config fallback from the context', async () => {
Expand All @@ -30,8 +25,7 @@ describe('useSWR - configs', () => {
await screen.findByText('data: 0')

// wait for the refresh interval
await act(() => sleep(INTERVAL * 1.5))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was misbehaving locally (flaky test, failed sometimes, passed others).

Would this be an acceptable swap? it doesn't really check anymore that we are over the INTERVAL time though, which might not be desirable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable!

screen.getByText('data: 1')
await screen.findByText('data: 1')
})

it('should stop revalidations when config.isPaused returns true', async () => {
Expand Down
44 changes: 43 additions & 1 deletion test/use-swr-suspense.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { act, fireEvent, screen } from '@testing-library/react'
import React, { ReactNode, Suspense, useEffect, useState } from 'react'
import React, {
ReactNode,
Suspense,
useEffect,
useReducer,
useState
} from 'react'
import useSWR, { mutate } from 'swr'
import {
createKey,
Expand Down Expand Up @@ -274,4 +280,40 @@ describe('useSWR - suspense', () => {
expect(startRenderCount).toBe(2) // fallback + data
expect(renderCount).toBe(1) // data
})

it('should return `undefined` data for falsy key', async () => {
const key = createKey()
const Section = ({ trigger }: { trigger: boolean }) => {
const { data } = useSWR(
trigger ? key : null,
() => createResponse('SWR'),
{
suspense: true
}
)
return <div>{data || 'empty'}</div>
}

const App = () => {
const [trigger, toggle] = useReducer(x => !x, false)
return (
<div>
<button onClick={toggle}>toggle</button>
<Suspense fallback={<div>fallback</div>}>
<Section trigger={trigger} />
</Suspense>
</div>
)
}

renderWithConfig(<App />)

await screen.findByText('empty')

fireEvent.click(screen.getByRole('button'))

screen.getByText('fallback')

await screen.findByText('SWR')
})
})