-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug?]: Cannot access context when read in Meta content prop. #41
Comments
Just to add my 2c, my experience is regarding Context on React. That said, in your example you don't define a default state for your context e.g:
With that change the meta tag ends up with it's description being "default ctx", if I had to take a stab at it, at the moment you try to render the root component, the only available value for your ctx is the one on the initial definition. If you were to move all the Head related stuff to a new component like so:
You'll be able to deal with the context in a better way, your approach feels more like an anti-pattern than a bug. |
I agree that reading context anywhere but in component body is usually a bad practice. Making it into a separate component solves the problem of course, so is inlining one: https://github.com/thetarnav/paraglide-solidstart-hackernews/blob/main/src/root.tsx#L25 I'm considering this a bug because reading ctx in any other prop will work fine: <Ctx.Provider value={...}>
<div class={useContext(Ctx)} />
</Ctx.Provider> thats because props are being read lazily in a computation, which will have access to the context when executed. <Ctx.Provider value={{ text: 'you should see this' }}>
{() => <Html lang="en">
{() => <>
<Head>
{() => <Meta name="description" content={() => useContext(Ctx).text} />}
</Head>
<Body>...</Body>
</>}
</Html>}
</Ctx.Provider> because props are compiled into getters and read lazily, so one could argue that there are many additional components between the provider and the ctx read. |
Yeah.. it's because of the lazy access of meta tags to insert them in the head. I see its in the head there but we don't really need to render them until we collect them all. I guess this is a solid meta issue. I will move there. |
any solutions for this? |
If you try to read context inside
content
prop ofMeta
component provided by start, the context will not be available, which is not expected as it's used directly under the provider.https://stackblitz.com/edit/solid-start-meta-context-issue?file=src%2Froot.tsx
I'm interested in working on a fix if this is something that's considered a bug.
The text was updated successfully, but these errors were encountered: