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: suspense when key is falsy #357

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/use-swr.ts
Expand Up @@ -566,6 +566,10 @@ function useSWR<Data = any, Error = any>(
throw latestError
}

if (!key) {
throw new Promise(() => {})
}

// return the latest data / error from cache
// in case `key` has changed
return {
Expand Down
55 changes: 55 additions & 0 deletions test/use-swr.test.tsx
Expand Up @@ -1267,6 +1267,61 @@ describe('useSWR - suspense', () => {
// 'suspense-7' -> undefined -> 'suspense-8'
expect(renderedResults).toEqual(['suspense-7', 'suspense-8'])
})

// hold render when suspense
it('should pause when key is falsy', async () => {
function SectionContent({ swrKey }) {
const { data } = useSWR(
() => (swrKey % 2 ? null : 'suspense-' + swrKey),
key =>
new Promise(res =>
setTimeout(() => res(key.replace('suspense-', '')), 100)
),
{
suspense: true
}
)
return <div>{data}</div>
}

const Section = () => {
const [key, setKey] = useState(9)
const handleClick = () => setKey(key + 1)

return (
<div onClick={handleClick}>
<Suspense fallback={<div>fallback</div>}>
<SectionContent swrKey={key} />
</Suspense>
</div>
)
}

const { container } = render(<Section />)

async function clickAndWait(duration) {
fireEvent.click(container.firstElementChild)
return await act(
() => new Promise(resolve => setTimeout(resolve, duration))
)
}

expect(
container.firstElementChild.lastElementChild.textContent
Copy link
Member Author

Choose a reason for hiding this comment

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

since <Suspense> might render inline styles onto children, then the html will look like

<div style="display: none !important">10</div>
<div>fallback</div>

so I always queried the content of last element child here. it would be wether the fallback div or the children with returned data.

@shuding I'm not sure if it's sth I implemented incorrectly or there's any other elegant way to work around. other suspense test cases seem don't have this problem

).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"10"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"12"`)
})
})

describe('useSWR - cache', () => {
Expand Down