-
Hello, I've just migrated from Remix to React Router. I am having some trouble reworking my routes from file system to routes.ts file. You can easily spot the repetitions here. And these are not all of my routes. I just paste some examples. I am wondering if I can DRY this language path param using nested routes or prefixes. I've read the docs but failed to get that param in my route module using any approach. The second question is about DRYing all the mdx legal routes. Is there a better way to achieve the same?
Thank you for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I would create a function that receives the first two arguments as const langs = ["de", "bg"];
function multiLangRoute(path: string, file: string) {
return [
route(path, file), // this is the default
...langs.map(lang => route(`${lang}/${path}`, addToFile(lang, file))
]
} Something kinda like that, implement addToFile yourself to add the language at the beginning of the file name. Then in your list of routes you can do: export default [
route(':lang?/', './routes/home.tsx', {index: true}),
route(':lang?/about', './routes/about.tsx'),
route(':lang?/bags/:id', './routes/bag.tsx'),
route(':lang?/bags', './routes/bags.tsx'),
route(':lang?/checkout', './routes/checkout.tsx'),
route(':lang?/contact', './routes/contact.tsx'),
route(':lang?/orders', './routes/orders.tsx'),
...multiLangRoute("delivery", "./legal/delivery.mdx"),
...multiLangRoute("payment", "./legal/payment.mdx"),
...multiLangRoute("privacy", "./legal/privacy.mdx"),
...multiLangRoute("return", "./legal/return.mdx"),
...multiLangRoute("terms", "./legal/terms.mdx"),
] satisfies RouteConfig |
Beta Was this translation helpful? Give feedback.
-
Nice. Thank you. I was wondering if React Router has something already build-in for similar use cases. What about these routes:
Can I DRY the |
Beta Was this translation helpful? Give feedback.
-
Thank you. I've managed to achieve routes that I like.
It's clean and declarative. Nice DSL by React Router :) |
Beta Was this translation helpful? Give feedback.
If there's a layout that all :lang routes share, then use
route
to define the parent route and rest of the routes as nested.If there's no layout and only the URL needs to include the param, use
prefix
helper.