S
SolidJS13mo ago
Max

SolidStart unknown number of optional nested route parameters

Hi, looking if there's a way to set up file based routes where there's an unknown number of optional nested routes. So could be /:a?/:b?/:c?/.../:f?... But seems might have to then be array of args cause we don't have names. So for now just going 3-4 levels explicitly like
[a]/
├─ [[b]]/
│ ├─ [[c]].tsx
[a]/
├─ [[b]]/
│ ├─ [[c]].tsx
which is anyway all I likely need so may be fine, but wondering if there was a way. Thanks
8 Replies
Max
Max13mo ago
I tried combining the nested and optional thing with catch all ... but unless I did it wrong I don't think that's the intended use for it
Jasi
Jasi13mo ago
did you call the route file [...allroutes].tsx? (allroutes can be replaced by anything) https://start.solidjs.com/core-concepts/routing#catch-all-routes This should explain it pretty well
Jasi
Jasi13mo ago
if you only want to have three levels, you have to work with folder names and index files. here, these routes work: /test/:anything /test/:anything/:anything /test/:anything/:anything/:anything
No description
Max
Max13mo ago
okay I think I get it, it just returns one params right? and then would have to split the string right? idk why was expecting to still get multiple params from useParams
Jasi
Jasi13mo ago
yes if you use the [...anything].tsx approach, the route parts will be combined. for example /firstpart/secondpart/anything
const params = useParams()

console.log(params.anything)
const params = useParams()

console.log(params.anything)
will return /firstpart/secondpart/anything
Max
Max13mo ago
okay great thank you
Jasi
Jasi13mo ago
you're welcome! If you're using the "fixed" sub routes approach, you can access the parts individually as expected. For my example above, it would be:
const params = useParams()

console.log(params.id)
console.log(params.otherid)
console.log(params.idthree)
const params = useParams()

console.log(params.id)
console.log(params.otherid)
console.log(params.idthree)
Max
Max13mo ago
yeaa awesome 🔥
Want results from more Discord servers?
Add your server