Skip to content

Commit 7b9d2e3

Browse files
authored
QueryParamTabs: don't show tab until loader is done (#3430)
I hit this while working on Active Problems. A query param tab with a loader gets rendered before its loader is actually done, and if it uses `usePrefetchedQuery`, that render triggers an error page which, oddly, goes away after the nav completes. This PR fixes it so the current tab stays on screen until the new tab's data has loaded. None of our existing `QueryParamTabs` pages run into this because their loaders fetch data for every tab up front. https://github.com/user-attachments/assets/f258ffac-e848-4151-afd5-6484b04b0cc8 The bug depends on the way RR's `useSearchParams` works internally. The return value of `useSearchParams` is [memoized based on `useLocation().search`](https://github.com/remix-run/react-router/blob/7ccdcecdd944e15be0cd0dcb702b86c9af1214a8/packages/react-router/lib/dom/lib.tsx#L2364-L2387), so every render gets the same object back until the location changes. When you click a tab, `QueryParamTabs` calls `set('tab', ...)` on that object, changing it in place, and then passes it to `setSearchParams`. That starts a navigation: the loader runs for the new URL, and the URL changes once it finishes. But as soon as the navigation starts, React Router rerenders the page to update the navigation state from idle to loading. In that render, `useLocation().search` hasn't changed (because the nav hasn't completed, because there's a loader), so `useSearchParams` returns the same memoized object, **which now has the new `tab` value in it,** which is what `QueryParamTabs` uses to decide which tab to render. So the new tab renders immediately instead of waiting for the loader. `usePrefetchedQuery` errors, and the error page shows until the navigation finishes. The fix is to pass a copy to `setSearchParams` instead, which leaves the memoized return value alone.
1 parent 94f1e9e commit 7b9d2e3

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { createRoutesStub, useLoaderData, useNavigation } from 'react-router'
9+
import { expect, test } from 'vitest'
10+
import { render } from 'vitest-browser-react'
11+
12+
import { Tabs } from '~/ui/lib/Tabs'
13+
14+
import { QueryParamTabs } from './QueryParamTabs'
15+
16+
test('waits for the loader before showing the new tab', async () => {
17+
let finishLoad = () => {}
18+
// tab each render of the second panel saw in loader data
19+
const panelTwoRenders: string[] = []
20+
21+
function PanelTwo() {
22+
const { tab } = useLoaderData<{ tab: string }>()
23+
panelTwoRenders.push(tab)
24+
return <p>Panel two</p>
25+
}
26+
27+
function Page() {
28+
// like the loading bar in RootLayout, which rerenders while the nav is pending
29+
const navigation = useNavigation()
30+
return (
31+
<QueryParamTabs defaultValue="one">
32+
<Tabs.List>
33+
<Tabs.Trigger value="one">One</Tabs.Trigger>
34+
<Tabs.Trigger value="two">Two</Tabs.Trigger>
35+
</Tabs.List>
36+
<Tabs.Content value="one">
37+
<p>Panel one</p>
38+
</Tabs.Content>
39+
<Tabs.Content value="two">
40+
<PanelTwo />
41+
</Tabs.Content>
42+
<p>Navigation {navigation.state}</p>
43+
</QueryParamTabs>
44+
)
45+
}
46+
47+
const Stub = createRoutesStub([
48+
{
49+
path: '/',
50+
Component: Page,
51+
// like a page loader that only prefetches the current tab's data
52+
loader: async ({ request }) => {
53+
const tab = new URL(request.url).searchParams.get('tab') ?? 'one'
54+
if (tab === 'two') await new Promise<void>((resolve) => (finishLoad = resolve))
55+
return { tab }
56+
},
57+
},
58+
])
59+
60+
const screen = await render(<Stub />)
61+
await expect.element(screen.getByText('Panel one')).toBeVisible()
62+
63+
await screen.getByRole('tab', { name: 'Two' }).click()
64+
await expect.element(screen.getByText('Navigation loading')).toBeInTheDocument()
65+
// still on the old tab while the new tab's loader runs
66+
await expect.element(screen.getByText('Panel one')).toBeVisible()
67+
expect(panelTwoRenders).toEqual([])
68+
69+
finishLoad()
70+
await expect.element(screen.getByText('Panel two')).toBeVisible()
71+
expect(panelTwoRenders).not.toContain('one')
72+
})

‎app/components/QueryParamTabs.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ export function QueryParamTabs(props: TabsRootProps) {
2020
const value = searchParams.get('tab') || props.defaultValue
2121

2222
function onValueChange(newValue: string | number | null) {
23+
// Copy rather than mutate: the hook's object is what we read `value` from,
24+
// so mutating it switches the tab on the next render, before the navigation
25+
// (and the loader it triggers) has committed.
26+
const nextParams = new URLSearchParams(searchParams)
2327
if (newValue === props.defaultValue) {
24-
searchParams.delete('tab')
28+
nextParams.delete('tab')
2529
} else {
26-
searchParams.set('tab', String(newValue))
30+
nextParams.set('tab', String(newValue))
2731
}
28-
setSearchParams(searchParams, { replace: true })
32+
setSearchParams(nextParams, { replace: true })
2933
}
3034

3135
return <Tabs.Root {...props} value={value} onValueChange={onValueChange} />

0 commit comments

Comments
 (0)