Replies: 3 comments 6 replies
-
Ideally it should be part of the the route definition (somehow) seeing how RR7 already generates types from that. But does it really give you anything if you still must validate (something that should not be part of RR) the provided information to avoid runtime errors? route('contacts', 'contacts.route.tsx', [
route(':id(number)', 'contact.route.tsx', [
route(':status(enum:pending|approved|rejected', 'contact-status.routes.tsx'),
])
]) |
Beta Was this translation helpful? Give feedback.
-
My first approach would be to define multiple routes per param value export default [
route("transactions/pending/details", "file.tsx"),
route("transactions/approved/details", "file.tsx"),
route("transactions/rejected/details", "file.tsx"),
] But you can't re-use a file, it would be nice if we could pass params to the file path export default [
route("transactions/pending/details", "file.tsx?status=pending"),
route("transactions/approved/details", "file.tsx?status=approved"),
route("transactions/rejected/details", "file.tsx?status=rejected"),
] Then RR could pass |
Beta Was this translation helpful? Give feedback.
-
How do you envision For example both route So unless this can be done in a way where it does not need to be repeated, it isn't any different than the current approach of writing schemas and validating params in loader/action or their client equivalents. Unless of course I am missing something from your explanation. |
Beta Was this translation helpful? Give feedback.
-
Dynamic params are a powerful way to render content based on dynamic URL segments, in React Router you can make a segment of the URL dynamic by adding a colon in front of the segment,
For example in
/transactions/:transactionId/details
/transactions
is a static segment, only matches exact same characters/:transactionId
is a dynamic segment, it matches anything/details
is a static segment, only matches exact same charactersso with that structure the route can match for following URLs:
Which is amazing, because you can use the
params.transactionId
from the Component, loaders and actions to fetch different data that has the same structure.But often, you have pages, that are dynamic but constrained, meaning, the page should only be shown when the dynamic segment is one of some values, not any values.
For example, you have a page to show a list of user onboardings, you want separate pages for UX purposes with same UI and structure, but their data will be different based on the status of these onboardings, for example we have
"pending" | "approved" | "rejected"
statuses.In this instance, I'd make my route as
/onboardings/:status
and in my loader, action or Component, you'd have to use something like zod to validate the
params.status
, and if it doesn't match, throw a 404 response:This is fine, but when you need this in other places like action or Component, or other data functions, especially when you have many routes like this, you realize an abstraction on RR side will make this much easier.
The approach I have in mind:
Generate possible values
A function
generateStaticParams
returns an object where each property is a dynamic segment name from current route and the value is an array of strings that can be narrowed down to a set of values for typing purposes:If a key is left out from the returned object, it'll have its initial type which is
string
orstring | undefined
Pros
Note
This is similar but different to pre-rendering and its purely for the business logic purposes as a convenience and type safety feature
There is also another approach that is mutually exclusive to the
generateStaticParams
, let's call this onevalidateDynamicParams
, this one receives the dynamic params of current route and returns a boolean indicating whether the URL with those dynamic params should match current route or not, effectively typing the params but this one runs on per-navigation basis, which might make a slight delay but effctively centeralizes the typing and validation of the dynamic params.Beta Was this translation helpful? Give feedback.
All reactions