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?)
<Route
path={translate('uri_select_login_auth_method')}
element={
<BarContextProvider>
<FooContextProvider>
<Content><AuthenticationMethodSelector mode="login" /></Content>
</FooContextProvider>
</BarContextProvider>
}
/>
<Route
path={translate('uri_select_login_auth_method')}
element={
<BarContextProvider>
<FooContextProvider>
<Content><AuthenticationMethodSelector mode="login" /></Content>
</FooContextProvider>
</BarContextProvider>
}
/>
17 Replies
high1
high12y ago
Hi, that should not be happening. Did you try to wrap your router with contexts?
Mathieu
MathieuOP2y ago
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>:
<Routes>
<I18nProvider namespaces={['auth']}>
<AuthRoutes />
</I18nProvider>

<I18nProvider namespaces={['finance']}>
<FinanceRoutes />
</I18nProvider>

<I18nProvider namespaces={['calendar']}>
<CalendarRoutes />
</I18nProvider>
</Routes>
<Routes>
<I18nProvider namespaces={['auth']}>
<AuthRoutes />
</I18nProvider>

<I18nProvider namespaces={['finance']}>
<FinanceRoutes />
</I18nProvider>

<I18nProvider namespaces={['calendar']}>
<CalendarRoutes />
</I18nProvider>
</Routes>
As those I18nProviders 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:
<>
<Route path=... element=... />
<Route path=... element=... />
<Route path=... element=... />
</>
<>
<Route path=... element=... />
<Route path=... element=... />
<Route path=... element=... />
</>
(I simplified a lot)
high1
high12y ago
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.
Mathieu
MathieuOP2y ago
The components rendered via the Routes from <AuthRoutes /> won't receive the context value from <I18nProvider namespaces={['auth']}> even though the context provider wraps the Routes 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).
high1
high12y ago
In the case that you've shown, contexts wrap different parts of component tree. Right? These have separate values
Mathieu
MathieuOP2y ago
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
high1
high12y ago
You're saying that you don't have context values in that sub part of the component tree, which is wrapped with same context?
Mathieu
MathieuOP2y ago
yes I don't have the values
high1
high12y ago
Ah, depends on how you navigate
Mathieu
MathieuOP2y ago
but I wonder if it's because of Navigate
high1
high12y ago
Can you show the code? Not literally, just the essence The navigation part
Mathieu
MathieuOP2y ago
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 internally
high1
high12y ago
Can't say for sure because of solid router But I don't think it should be happening, unless you are using data functions
Mathieu
MathieuOP2y ago
Fyi I'm not using data functions
Otonashi
Otonashi2y ago
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 essentially
Mathieu
MathieuOP2y ago
Thank 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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server