Solid Router within Router
Hi I am encountering something weird and can't figure out either what I am doing wrong.
I have an initial Router a the App.tsx level like so:
<Router base="/">
<Route path="/" component={StartApp} />
<Route path="/contract" component={ContractLifecycle}/>
<Route path="*404" component={()=> <div>Page Not found {useLocation().pathname}!</div>} />
</Router>
Then under ContractLifecycle I have another router like so:
<Router base="contract/">
<Route path="/" component={LoadEncode}/>
<Route path="/prepare" component={Prepare}/>
</Router>
When I load the App, the StartApp component loads fine, but when I navigate to "contract/", the LoadEncode component doesn't load, which is what I would have expected (same as / loading for the App router). If however I reload the page (click reload in the browser) then the LoadEncode component page shows up.
I also tried without the "/" at the end like so:
<Router base="contract">
<Route path="" component={LoadEncode}/>
<Route path="/prepare" component={Prepare}/>
</Router>
But the result is the same.16 Replies
GitHub
GitHub - solidjs/solid-router: A universal router for Solid inspire...
A universal router for Solid inspired by Ember and React Router - solidjs/solid-router
Hmm I wonder how I make this work, the problem is not so much the nested routes, I could declre the routes without the Router wrapper but I want those nested routes to be within a Context, and having Route wrapped in a context doesn't seem to work that well, it works for Router but not Route.
If I wrap the routes in a Context Provider what I get on the screen is:
[object Object][object Object]
Because its trying to render the routes which obviously dooesn't work that well.
Maybe I need to put some other logic in my Contexct provider and rather than just doing
<ContractContext.Provider value={value}>
{props.children}
</ContractContext.Provider>
I should maybe handle the props.children that are routes differently, but I don't kno whow 😩but I want those nested routes to be within a ContextWhy is it important to restrict access to that context to specific routes?
Its more a segregation of concerns thing rather than anything else, I could put the context at the top level but then it feels like I am shoving everything at the top of the tree. Which could end up being very hard to maintain in the long run. The idea here was to have lets say a "sub-functionality" in the web-app that had its own context and its own routes. Makes things cleaner and easier to maintain as independent entities.
I guess I could always create a master context, and then "attach" other contexts to that through a map or something, and the always useContext on the master context, and pull the sub-context from that, possibly even ad an remove the sub-context through onMount and onCleanup or something but would have been nicer if I could do it in the SolidJS framework.
SolidStart supports what you are looking for via layouts for nested routes.
Now I'm going out on a limb here:
If you write the provider to only initialize once regardless how many times you call it you could just put the provider at the root of the components that need it.
I'm still trying to figure out how to emulate nested route layouts with a vanilla router ...
Once you've got the provider sorted out (initializes only once) you can pull the provider into a layout:
Rough sketch:
That could indeed be an option, I'll see if I can get the provider to only initialize once. Not as elegant as I would have wanted but it could indeed work.
Frequently elegant isn't equivalent to resilient.
I'd beg to differ, but I think that might lead us down a long winded mostly philosophical debate.
That being said I was able to do what you proposed. Had to externalize some properties etc. but it works, so thats cool.
Thanks for the assist!
make it work, make it right, make it fast
Here you go - you can specify a layout component at the wrapper level. I just wasn't sure.
https://stackblitz.com/edit/stackblitz-starters-p8qprb?file=src%2Fapp.tsx
peerreynders
StackBlitz
solidjs/router Layout for nested routes - StackBlitz
A Solid TypeScript project based on @solidjs/meta, @solidjs/router, solid-js, typescript, vite and vite-plugin-solid
@MikeM42 I also stumbled on the issue regarding nested routers and figured out a (experimental) workaround, let me know if you wanna see it 😅
Always interested yes, would be great to see if there's a way to solve this.
Here ya go 🙂
Its very experimental. It acts like a layer that proxies outer-router navigation events to the NestedRouter and vice-versa 😅
You would use it e.g. like so:
That does look pretty cool! I think I may have a use for this, will let you know when I've had a chance to try it out.
How do you declare the Route to the component under which you declare the nested route though? You must declare it in some way that it will render any component unde /xyz/something through the component that holds the nested route, no?
In my case the parent router is SolidStart's fs router, so I literally create a fs based route and put my NestedRouter inside of it 😅 , eg.
I think I see, it didn't quite work for what I had in mind so will need to experiment a bit more on how to integrate it in my flow.
Many thanks for your help though.
I see 😅, I hope you find a solution that works for your use case!