Skip to content

fix: update error handling in MatchInner to access error data directly #4746

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/router/framework/react/guide/not-found-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const Route = createFileRoute('/posts/$postId')({
})
return { post }
},
// `data: unknown` is passed to the component via the `data` option when calling `notFound`
// `data: any` is passed to the component via the `data` option when calling `notFound`
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this any instead of unknown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because the type of notFoundRouteProps was changed from unknown to Partial<NotFoundError>,
and the data in NotFoundError is currently typed as any.

Copy link
Contributor

Choose a reason for hiding this comment

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

can we change it there to unknown? to force people to add some runtime type checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you mean it should be reverted the change?

Copy link
Contributor Author

@leesb971204 leesb971204 Aug 2, 2025

Choose a reason for hiding this comment

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

As I menthioned here, notFoundRouteProps can contain not only data but also any value of type NotFoundError like routeId.

or is it appropriate to convert the data type of NotFoundError to unknown?

notFoundComponent: ({ data }) => {
// ❌ useLoaderData is not valid here: const { post } = Route.useLoaderData()

Expand Down
4 changes: 2 additions & 2 deletions packages/react-router/src/renderRouteNotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function renderRouteNotFound(
) {
if (!route.options.notFoundComponent) {
if (router.options.defaultNotFoundComponent) {
return <router.options.defaultNotFoundComponent data={data} />
return <router.options.defaultNotFoundComponent {...data} />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part causes the structure to become nested like data: { data: ... }.

}

if (process.env.NODE_ENV === 'development') {
Expand All @@ -23,5 +23,5 @@ export function renderRouteNotFound(
return <DefaultGlobalNotFound />
}

return <route.options.notFoundComponent data={data} />
return <route.options.notFoundComponent {...data} />
}
118 changes: 117 additions & 1 deletion packages/react-router/tests/not-found.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
createRootRoute,
createRoute,
createRouter,
notFound,
} from '../src'
import type { RouterHistory } from '../src'
import type { NotFoundRouteProps, RouterHistory } from '../src'

let history: RouterHistory

Expand Down Expand Up @@ -123,3 +124,118 @@ test.each([
expect(notFoundComponent).toBeInTheDocument()
},
)

test('defaultNotFoundComponent and notFoundComponent receives data props via spread operator', async () => {
const customData = {
message: 'Custom not found message',
}

const DefaultNotFoundComponentWithProps = (props: NotFoundRouteProps) => (
<div data-testid="default-not-found-with-props">
<span data-testid="message">{props.data.message}</span>
</div>
)

const NotFoundComponentWithProps = (props: NotFoundRouteProps) => (
<div data-testid="not-found-with-props">
<span data-testid="message">{props.data.message}</span>
</div>
)

const rootRoute = createRootRoute({
component: () => (
<div data-testid="root-component">
<h1>Root Component</h1>
<div>
<Link
data-testid="default-not-found-route-link"
to="/default-not-found-route"
>
link to default not found route
</Link>
<Link data-testid="not-found-route-link" to="/not-found-route">
link to not found route
</Link>
</div>
<Outlet />
</div>
),
})

const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => (
<div data-testid="index-component">
<h2>Index Page</h2>
</div>
),
})

const defaultNotFoundRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/default-not-found-route',
loader: () => {
throw notFound({ data: customData })
},
component: () => (
<div data-testid="default-not-found-route-component">
Should not render
</div>
),
})

const notFoundRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/not-found-route',
loader: () => {
throw notFound({ data: customData })
},
component: () => (
<div data-testid="not-found-route-component">Should not render</div>
),
notFoundComponent: NotFoundComponentWithProps,
})

const router = createRouter({
routeTree: rootRoute.addChildren([
indexRoute,
defaultNotFoundRoute,
notFoundRoute,
]),
history,
defaultNotFoundComponent: DefaultNotFoundComponentWithProps,
})

render(<RouterProvider router={router} />)
await router.load()
await screen.findByTestId('root-component')

const defaultNotFoundRouteLink = screen.getByTestId(
'default-not-found-route-link',
)
defaultNotFoundRouteLink.click()

const defaultNotFoundComponent = await screen.findByTestId(
'default-not-found-with-props',
{},
{ timeout: 1000 },
)
expect(defaultNotFoundComponent).toBeInTheDocument()

const defaultNotFoundComponentMessage = await screen.findByTestId('message')
expect(defaultNotFoundComponentMessage).toHaveTextContent(customData.message)

const notFoundRouteLink = screen.getByTestId('not-found-route-link')
notFoundRouteLink.click()

const notFoundComponent = await screen.findByTestId(
'not-found-with-props',
{},
{ timeout: 1000 },
)
expect(notFoundComponent).toBeInTheDocument()

const errorMessageComponent = await screen.findByTestId('message')
expect(errorMessageComponent).toHaveTextContent(customData.message)
})
10 changes: 6 additions & 4 deletions packages/router-core/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
RouteMatch,
} from './Matches'
import type { RootRouteId } from './root'
import type { ParseRoute, RouteById, RoutePaths } from './routeInfo'
import type { ParseRoute, RouteById, RouteIds, RoutePaths } from './routeInfo'
import type { AnyRouter, RegisteredRouter } from './router'
import type { BuildLocationFn, NavigateFn } from './RouterProvider'
import type {
Expand Down Expand Up @@ -1322,9 +1322,11 @@ export type ErrorComponentProps<TError = Error> = {
info?: { componentStack: string }
reset: () => void
}
export type NotFoundRouteProps = {
// TODO: Make sure this is `| null | undefined` (this is for global not-founds)
data: unknown

export type NotFoundRouteProps<TData = unknown> = {
data?: TData
isNotFound: boolean
routeId: RouteIds<RegisteredRouter['routeTree']>
Comment on lines +1326 to +1329
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated this type.

This enforces stricter type checking on data, while still allowing type inference when users provide a generic to specify the type explicitly.
Also, isNotFound and routeId were missing, so they have been added.

Copy link
Contributor Author

@leesb971204 leesb971204 Aug 3, 2025

Choose a reason for hiding this comment

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

@schiller-manuel

In my opinion, the data property of NotFoundError should allow any value, since users might pass arbitrary data when using the notFound function.
So I think it would be more appropriate to modify only NotFoundRouteProps.

can you please check this change?

}

export class BaseRoute<
Expand Down
4 changes: 2 additions & 2 deletions packages/solid-router/src/renderRouteNotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function renderRouteNotFound(
) {
if (!route.options.notFoundComponent) {
if (router.options.defaultNotFoundComponent) {
return <router.options.defaultNotFoundComponent data={data} />
return <router.options.defaultNotFoundComponent {...data} />
}

if (process.env.NODE_ENV === 'development') {
Expand All @@ -22,5 +22,5 @@ export function renderRouteNotFound(
return <DefaultGlobalNotFound />
}

return <route.options.notFoundComponent data={data} />
return <route.options.notFoundComponent {...data} />
}
Loading