S
SolidJS

support

How to have a default layout for all routes?

Ppronoob7/3/2023
I am using @solidjs/router and I want to have a default layout to have a default background color. How do I do that? Code:
render(
() => (
<Router>
<Routes>
<Route path="/" component={Index} />
<Route path="/help" component={Help} />
</Routes>
</Router>
),
root!
);
render(
() => (
<Router>
<Routes>
<Route path="/" component={Index} />
<Route path="/help" component={Help} />
</Routes>
</Router>
),
root!
);
GGrahf7/3/2023
In my project I have index.jsx like this:
render(
() => (
<Router>
<App />
</Router>
),
document.getElementById('root')
)
render(
() => (
<Router>
<App />
</Router>
),
document.getElementById('root')
)
Then in App.jsx
return (
<div

class='bg-red-500'
>
<Nav />
<Routes>
<Route path='/' element={<Home />} />
<Route path='/someplace/:param1/:param2' element={<SomeComp />} />
<Route path='/*' element={<Navigate href='/' />} />
</Routes>
</div>
)
return (
<div

class='bg-red-500'
>
<Nav />
<Routes>
<Route path='/' element={<Home />} />
<Route path='/someplace/:param1/:param2' element={<SomeComp />} />
<Route path='/*' element={<Navigate href='/' />} />
</Routes>
</div>
)
Mmdynnl7/4/2023
this simply works. if you need a more robust approach, see nested routes https://github.com/solidjs/solid-router#nested-routes
GGrahf7/4/2023
ok
Ppronoob7/4/2023
Thank you so much! ❤️ Robust? How is using nested route better? Can you give an example? I just cannot seem to get them to work This is what I tried:
<Router>
<Route path="/" component={Layout}>
...
</Route>
<Router />
<Router>
<Route path="/" component={Layout}>
...
</Route>
<Router />
For some reason, hot reload does not work with this approach properly, you have to manually refresh to see changes
Mmdynnl7/4/2023
say if some routes are public and some private like auth
<Route path="/auth" component={() => <div>auth: <Outlet/></div>}>
<Route path="/foo" element="foo" />
</Route>
<Route path="/" component={() => <div>public: <Outlet/></div>}>
<Route path="/bar" element="bar" />
</Route>
<Route path="/auth" component={() => <div>auth: <Outlet/></div>}>
<Route path="/foo" element="foo" />
</Route>
<Route path="/" component={() => <div>public: <Outlet/></div>}>
<Route path="/bar" element="bar" />
</Route>
Ppronoob7/4/2023
<Router>
<Routes>
<Route
path="/"
component={() => (
<div class="bg-sky-500 h-screen">
<Outlet></Outlet>
</div>
)}
>
<Route path="/login" component={Login} />
</Route>
</Routes>
</Router>
<Router>
<Routes>
<Route
path="/"
component={() => (
<div class="bg-sky-500 h-screen">
<Outlet></Outlet>
</div>
)}
>
<Route path="/login" component={Login} />
</Route>
</Routes>
</Router>
This does not work at all ^ It only works for the /login route (shows bg-sky-500 with h-screen) But not for the / route
Mmdynnl7/4/2023
nested routes need a leaf child route to render a route with empty path would do for top level site layout i'd go with what @Grahf posted, then use nested routes for auth etc
Ppronoob7/4/2023
But hot reload does not work with @Grahf's solution for some reason
Mmdynnl7/4/2023
doe hmr work in other places?
Ppronoob7/4/2023
If I remove the top-level layout, it works fine
<Router>
<div class="bg-sky-500 h-screen">
<Routes>
<Route path="/" component={Root} />
<Route path="/login" component={Login} />
</Routes>
</div>
</Router>
<Router>
<div class="bg-sky-500 h-screen">
<Routes>
<Route path="/" component={Root} />
<Route path="/login" component={Login} />
</Routes>
</div>
</Router>
The page is not reloaded automatically
Mmdynnl7/4/2023
hmm, could you move the div out the Router component?
Ppronoob7/4/2023
Nope, still does not work I have switched so many libraries and frameworks because of this issue, Svelte seems to do it perfectly, but I hate few things about it, I would appreciate it if someone can help me
Mmdynnl7/4/2023
i'll try to repro it locally fyi hmr for context is still wip i think
Ppronoob7/4/2023
Tysm! Please mention me if you find anything
Mmdynnl7/4/2023
@pronoob0 seems to be working for me, what's your setup exactly
Ppronoob7/4/2023
I am on a mac and I used degit typescript template (manually installed tailwind)
Mmdynnl7/4/2023
hmm, on a mac as well, and took the same step as and still working fine for me, i don't mind a couple refresh though so our expectations might differ the only thing that stands out to me is duplicate mounting on index.tsx even though @refresh reload pragma exists https://codesandbox.io/p/github/mdynnl/solid-ts
Ppronoob7/4/2023
I am not importing tailwind.css file, is that a problem? I am just using the @tailwind directives instead and importing the index.css file Also, I am directly passing the router to render(), I don't think that's a problem:
render(
() => (
<Router>
<div class="bg-pink-500 h-screen">
<Routes>
<Route path="/" component={Root} />
<Route path="/login" component={Login} />
</Routes>
</div>
</Router>
),
root!
);
render(
() => (
<Router>
<div class="bg-pink-500 h-screen">
<Routes>
<Route path="/" component={Root} />
<Route path="/login" component={Login} />
</Routes>
</div>
</Router>
),
root!
);
I have to manually refresh the browser to see changes for top-level layout, that's the only problem I am having right now I am just gonna give up on SolidJS for now, unfortunately What do you recommend as an alternative? I am thinking of Preact or something
Mmdynnl7/5/2023
not really sure but render() needs to be a separate file as it's necessary to reload the whole page
Ppronoob7/5/2023
If that's the reason why it wasn't working, that would be the stupidest thing the docs re ommend
Mmdynnl7/5/2023
probably because it was written long before hmr is implemented and hasn't updated ever since
Ppronoob7/6/2023
It worked! Thank you so much for saving so much of my time!

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Why does effect re-fire? How to use createStore values in effect correctly?Check out this component. Why does the effect refire when I'm only changing person.name.first? It Access native html dialogs show/close methods in clickHandlerHey folks, I am currently using native html dialogs inside my app and am accessing their show/closeUncaught ReferenceError: createDeepSignal is not definedNot really sure what I'm doing wrong here. I definitely have SolidJS >v1.5 and I'm using a close co__vite_ssr_import_0__.default is not a function only on API endpointsI am getting an error from my api end-points when importing a third party library in my API code. I Why is storageSignal possibly null?I am having trouble understanding why TypeScript thinks that this storageSignal `theme` can be null:Is there a way to debug how callbacks are called, when firing an event with testing-library?[SOLVED]Hey, I am having a hard time testing my code and would like to find out if and how some of my eventUnderstanding Suspense in SolidStart SSRTo understand how Suspense behave in a SolidStart SSR project, I have project created using the SoliSolidStart debugging in VSCodeHow can I debug (mean put breakpoints and step over code) SolidStart project in VSCode? I am currenWhat's the best way to make a docs website?I'm building a SolidJS library and want to make a simple docs website for it. I'm hoping to leveragesolidjs/testing-library using `location` but still getting <Router /> errorHey, I am currently trying to setup on of my solid-apps with solidjs/testing-library. I want to tesAny way to get the status of a multipart/form-data file upload?I would like to be able to give the user some indication of how much of their file has completed uplregex in useMatchIs there a way to use a regex in useMatch? I would like to match `/book/2` ... but not just `/book`How to deploy unplugin-icons on production?For a SolidStart project, I'm using this library https://github.com/antfu/unplugin-icons as Icon CMake a Resource depend on another ResourceI would like to have a resource depend (awaiting) on another, like so: ```ts const [resource2] = cvanilla js in onMount?I got help recently about finding a way to import a vanilla javascript file as an independent sourceEmpty event with useRequest()I'm getting an empty event (no properties on the object) via `useServerContext()` even when `isServeExample of using createWebSocketServer within an API endpoint with SSR: falseIs there a simple example of using createWebSocketServer within an API enpoint? Current `with-websocSolid Bootstrap Data TableHi Im looking for a custom solid-js data table solution. To minimize my workload, of course I beliHow to get `useParams` in the "root.tsx" in a server-rendered-page.Inside the `root.tsx` before any `FileRoutes` are resolved, how can we get the full path / the param[Solid-start]FileRoutes] is there a way of using two separate Routes folders in `root.tsx`E.g., my main content should be Client-Side rendered, but the headers should be server side rendered