Skip to content

Commit 41e37f8

Browse files
authored
fix: use loader data goes undefined (#7265)
1 parent 2231d53 commit 41e37f8

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎.changeset/nine-years-grab.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/solid-router': patch
3+
---
4+
5+
Fix for useLoaderData sometimes going undefined

‎packages/solid-router/src/useMatch.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,21 @@ export function useMatch<
109109
return Solid.createMemo((prev: TSelected | undefined) => {
110110
const selectedMatch = match()
111111

112-
if (selectedMatch === undefined) return undefined
112+
if (selectedMatch === undefined) {
113+
const hasPendingMatch = opts.from
114+
? Boolean(router.stores.pendingRouteIds.get()[opts.from!])
115+
: (nearestMatch?.hasPending() ?? false)
116+
117+
if (
118+
prev !== undefined &&
119+
(hasPendingMatch || router.stores.isTransitioning.get())
120+
) {
121+
return prev
122+
}
123+
124+
return undefined
125+
}
126+
113127
const res = opts.select ? opts.select(selectedMatch as any) : selectedMatch
114128
if (prev === undefined) return res as TSelected
115129
return replaceEqualDeep(prev, res) as TSelected

‎packages/solid-router/tests/loaders.test.tsx‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createRootRoute,
1212
createRoute,
1313
createRouter,
14+
useLoaderData,
1415
useRouter,
1516
} from '../src'
1617

@@ -741,6 +742,51 @@ test('clears pendingTimeout when match resolves', async () => {
741742
expect(fooPendingComponentOnMountMock).not.toHaveBeenCalled()
742743
})
743744

745+
test('useLoaderData retains previous data while route match is pending', async () => {
746+
const history = createMemoryHistory({ initialEntries: ['/app'] })
747+
const rootRoute = createRootRoute({
748+
component: () => {
749+
const loaderData = useLoaderData({ from: '/app' })
750+
751+
return (
752+
<>
753+
<div data-testid="combined">{`${loaderData().length}:0`}</div>
754+
<Outlet />
755+
</>
756+
)
757+
},
758+
})
759+
const appRoute = createRoute({
760+
getParentRoute: () => rootRoute,
761+
path: '/app',
762+
loader: () => 'loaded',
763+
component: () => <div>App route</div>,
764+
})
765+
const routeTree = rootRoute.addChildren([appRoute])
766+
const router = createRouter({ routeTree, history })
767+
768+
render(() => <RouterProvider router={router} />)
769+
770+
expect(await screen.findByTestId('combined')).toHaveTextContent('6:0')
771+
772+
const appMatch = router.state.matches.find(
773+
(match) => match.routeId === '/app',
774+
)
775+
776+
expect(appMatch).toBeDefined()
777+
778+
if (!appMatch) {
779+
throw new Error('Expected /app match to be active')
780+
}
781+
782+
router.stores.setPending([{ ...appMatch, id: `${appMatch.id}__pending` }])
783+
router.stores.setMatches(
784+
router.state.matches.filter((match) => match.routeId !== '/app'),
785+
)
786+
787+
expect(screen.getByTestId('combined')).toHaveTextContent('6:0')
788+
})
789+
744790
test('cancelMatches after pending timeout', async () => {
745791
function getPendingComponent(onMount: () => void) {
746792
const PendingComponent = () => {

0 commit comments

Comments
 (0)