Skip to content

Commit

Permalink
fix: do not suspend on subsequent page loads (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
swandir committed Jun 2, 2020
1 parent e5f0bb3 commit 342c1fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/tests/usePaginatedQuery.test.js
Expand Up @@ -192,4 +192,39 @@ describe('usePaginatedQuery', () => {
rendered.getByText('Data second-search 1')
await waitForElement(() => rendered.getByText('Data second-search 2'))
})

it('should not suspend while fetching the next page', async () => {
function Page() {
const [page, setPage] = React.useState(1)

const { resolvedData } = usePaginatedQuery(
['data', { page }],
async (queryName, { page }) => {
await sleep(1)
return page
},
{
initialData: 0,
suspense: true,
}
)

return (
<div>
<h1 data-testid="title">Data {resolvedData}</h1>
<button onClick={() => setPage(page + 1)}>next</button>
</div>
)
}

// render will throw if Page is suspended
const rendered = render(
<ReactQueryCacheProvider>
<Page />
</ReactQueryCacheProvider>
)

fireEvent.click(rendered.getByText('next'))
await waitForElement(() => rendered.getByText('Data 2'))
})
})
8 changes: 5 additions & 3 deletions src/usePaginatedQuery.js
Expand Up @@ -39,12 +39,14 @@ export function usePaginatedQuery(...args) {
status = 'success'
}

handleSuspense(query)

return {
const paginatedQuery = {
...query,
resolvedData,
latestData,
status,
}

handleSuspense(paginatedQuery)

return paginatedQuery
}

0 comments on commit 342c1fb

Please sign in to comment.