Replies: 1 comment
-
An implementation of Solution 2 is available: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Current problem
Currently the
notFound()
function accepts a params object with the shape ofNotFoundError
that is then passed as props to thenotFoundComponent
.Arbitrary data can be sent through this mechanism with the
data?: any
key ofNotFoundError
, but this any prevents us from creating type-safe "not found" errors.router/packages/react-router/src/not-found.tsx
Lines 8 to 23 in 2088be2
Goals
Some applications might need to present a different version of the
notFoundComponent
depending on how/where/why thenotFound
error was thrown. We can imagine a few cases:Each of the above examples might require some custom props to function properly, in addition to requiring a way to determine which of these to display (assuming an app that uses all 3).
For this reason, this RFC proposes that we should allow users to type the
data
key.Possible solutions
We consider that it is not the role of the
notFoundComponent
to handle this variety of cases, and each route should be responsible for displaying such screens. Users can define their own component and pass props to it, there is no need to complexify the route API.// no code sample, this solution changes nothing
We allow users to type the
data
key globally, through interface augmentation. Most apps probably don't need a huge variety of "not found" screens, and so simple patterns, globally declared, are probably enough. For example:tanstack/router should be the most type-safe router out there, so we need to strongly type the
data
key on a per-route basis. This can be done through inferring thedata
type from theerrorComponent
props, and enforce those types using therouteId
key of theNotFoundError
Considerations
data
key is typed asany
, and so changing the type to anything else might cause type errors in some applications. However, all 3 of the solutions proposed above can be implemented with no (or minimal) implications for this.Beta Was this translation helpful? Give feedback.
All reactions