File based routing - pass props from nested layout to children
I have an app where the outer/wrapper/layout component has state stored as signals, and then the different routes have children which also use this state so the signals are passed down via props. This worked fine as an SPA but I am migrating to SolidStart so that I can make use of SSG and SSR and as far as I can tell it is not possible to pass props down to child components when using file-based-routing "nested layouts", i.e. with
props.children. I've not used nextjs or anything similar, so is my first time using a metaframework with file based routing and I'm not sure if this the normal/typical behaviour for nested layouts and how this is worked around.Routing - SolidStart Docs
Build your SolidStart app with file-based routing. Create pages, nested layouts, and dynamic routes with simple file structure.
3 Replies
I'd recommend creating a context for the layout that is consumed in the child routes and provides all the necessary state, you're correct that Solid Router doesn't let you pass props to child routes
Thanks, I will do that.
I'm thinking of taking a different approach to avoid the complexity of having to break things up and create contexts for the signals I'm using. I'm thinking about avoiding the file-based-routing and just having a single routes/index.tsx and simply render different content depending on the values from
useParams.
As far as I can tell there isn't any disadvantages to this, other than I expect the child components won't be code split, but that actually is preferrable for me.
Is there any reason I shouldn't do this?
Ah it seems that without the routes defined by the file system, useParams can't find the relevant param values, which makes sense, and SolidStart only works with file based routing so I would need to use useLocation instead and parse the values manually. Not the end of the world I guess.You can define the routes manually with
<Route>, but the same props restriction will still apply since that's all handled by Solid Router, not Start