Solid Router and losing Contexts
When navigating to a route, I noticed I lose all the context values. I have to wrap the component (that needs to rendered for the route) again by all the context providers, and for every route.
Is this normal? It seems awkward. (of course I can create some generic component that wraps the component to render for that route with all the context providers again, but is it normal in the first place that I lose the contexts? If so, how do you approach this?)
17 Replies
Hi, that should not be happening.
Did you try to wrap your router with contexts?
The whole problem seems to be that the context providers should be wrapping
<Routes>
, and they won't work if I wrap only specific sets of <Route>
s inside <Routes>
:
As those I18nProvider
s don't provide the context values for the routed content, I have to wrap each individual Route
by a context provider.
By the way components such <AuthRoutes>
contain something like:
(I simplified a lot)That's correct - if you wrap a part of the tree with one context, other parts of component tree will not see it.
These contexts are separate, so if you need common data - it suggests that you need to hoist these contexts.
The components rendered via the
Route
s from <AuthRoutes />
won't receive the context value from <I18nProvider namespaces={['auth']}>
even though the context provider wraps the Route
s as shown above.
The providers have no effect. Only if I move the provider above Routes
, but the whole idea was to have different context values for different set of routes.
More specifically in this example, namespace translations per sub-app (e.g. the calendar app is rendered, load the translations only for the calendar app).In the case that you've shown, contexts wrap different parts of component tree. Right?
These have separate values
yes, for example the I18n context provider wraps a set of routes relative to authentication components. But the translations won't be found
There's actually a navigate call from another part of the application. it might be the reason
You're saying that you don't have context values in that sub part of the component tree, which is wrapped with same context?
yes I don't have the values
Ah, depends on how you navigate
but I wonder if it's because of Navigate
Can you show the code?
Not literally, just the essence
The navigation part
Actually that's what I'm saying. I tested without any navigate. Context providers inside
<Routes>
won't work for the content rendered for a <Route>
. The context values are available only if the provider wraps the <Routes>
component.
I think this has something to do with how routes/route work internallyCan't say for sure because of solid router
But I don't think it should be happening, unless you are using data functions
Fyi I'm not using data functions
you can't wrap only some routes because nothing actually gets executed in the context
if i'm not mistaken all that happens is
Route
returns the data required to render itself and Routes
actually does the rendering, or something similar to that, so any contexts that wrap only Route
can't be read since the reading happens outside the context essentiallyThank you @otonashi9 < 3 that fully answers the question here.
FYI I kinda continued the discussion here:
https://github.com/solidjs/solid-router/issues/273#issuecomment-1580054155
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View