Skip to content

Commit e974ee5

Browse files
authored
fix(solid-router): make useLoaderDeps return Accessor for reactivity (#5348)
1 parent a7db57c commit e974ee5

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

packages/solid-router/src/useLoaderDeps.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useMatch } from './useMatch'
2+
import type { Accessor } from 'solid-js'
23
import type {
34
AnyRouter,
45
RegisteredRouter,
@@ -27,20 +28,20 @@ export type UseLoaderDepsRoute<out TId> = <
2728
TSelected = unknown,
2829
>(
2930
opts?: UseLoaderDepsBaseOptions<TRouter, TId, TSelected>,
30-
) => UseLoaderDepsResult<TRouter, TId, TSelected>
31+
) => Accessor<UseLoaderDepsResult<TRouter, TId, TSelected>>
3132

3233
export function useLoaderDeps<
3334
TRouter extends AnyRouter = RegisteredRouter,
3435
const TFrom extends string | undefined = undefined,
3536
TSelected = unknown,
3637
>(
3738
opts: UseLoaderDepsOptions<TRouter, TFrom, TSelected>,
38-
): UseLoaderDepsResult<TRouter, TFrom, TSelected> {
39+
): Accessor<UseLoaderDepsResult<TRouter, TFrom, TSelected>> {
3940
const { select, ...rest } = opts
4041
return useMatch({
4142
...rest,
4243
select: (s) => {
4344
return select ? select(s.loaderDeps) : s.loaderDeps
4445
},
45-
}) as UseLoaderDepsResult<TRouter, TFrom, TSelected>
46+
}) as Accessor<UseLoaderDepsResult<TRouter, TFrom, TSelected>>
4647
}

packages/solid-router/tests/route.test.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
21
import { cleanup, render, screen } from '@solidjs/testing-library'
2+
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
33

44
import {
55
RouterProvider,
@@ -204,6 +204,51 @@ describe('onEnter event', () => {
204204
})
205205
})
206206

207+
describe('useLoaderDeps', () => {
208+
test('returns an Accessor', async () => {
209+
const rootRoute = createRootRoute()
210+
const indexRoute = createRoute({
211+
getParentRoute: () => rootRoute,
212+
path: '/',
213+
loaderDeps: ({ search }) => ({ testDep: 'value' }),
214+
component: () => {
215+
const deps = indexRoute.useLoaderDeps()
216+
// deps should be an Accessor, so we need to call it to get the value
217+
expect(typeof deps).toBe('function')
218+
expect(deps()).toEqual({ testDep: 'value' })
219+
return <div>Index</div>
220+
},
221+
})
222+
const routeTree = rootRoute.addChildren([indexRoute])
223+
const router = createRouter({ routeTree, history })
224+
render(() => <RouterProvider router={router} />)
225+
const indexElem = await screen.findByText('Index')
226+
expect(indexElem).toBeInTheDocument()
227+
})
228+
229+
test('returns an Accessor via Route API', async () => {
230+
const rootRoute = createRootRoute()
231+
const indexRoute = createRoute({
232+
getParentRoute: () => rootRoute,
233+
path: '/',
234+
loaderDeps: ({ search }) => ({ testDep: 'api-value' }),
235+
component: () => {
236+
const api = getRouteApi('/')
237+
const deps = api.useLoaderDeps()
238+
// deps should be an Accessor, so we need to call it to get the value
239+
expect(typeof deps).toBe('function')
240+
expect(deps()).toEqual({ testDep: 'api-value' })
241+
return <div>Index with API</div>
242+
},
243+
})
244+
const routeTree = rootRoute.addChildren([indexRoute])
245+
const router = createRouter({ routeTree, history })
246+
render(() => <RouterProvider router={router} />)
247+
const indexElem = await screen.findByText('Index with API')
248+
expect(indexElem).toBeInTheDocument()
249+
})
250+
})
251+
207252
describe('route.head', () => {
208253
test('meta', async () => {
209254
const rootRoute = createRootRoute({

packages/solid-router/tests/routeApi.test-d.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ describe('getRouteApi', () => {
8888
>()
8989
})
9090
test('useLoaderDeps', () => {
91-
expectTypeOf(invoiceRouteApi.useLoaderDeps<DefaultRouter>()).toEqualTypeOf<{
92-
dep: number
93-
}>()
91+
expectTypeOf(invoiceRouteApi.useLoaderDeps<DefaultRouter>()).toEqualTypeOf<
92+
Accessor<{
93+
dep: number
94+
}>
95+
>()
9496
})
9597
test('useMatch', () => {
9698
expectTypeOf(invoiceRouteApi.useMatch<DefaultRouter>()).toEqualTypeOf<

0 commit comments

Comments
 (0)