You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using the render method from @builder.io/qwik to dynamically generate components on a page based on a CMS returned HTML.
Unfortunately any attempt to call useContext(...) in those fail with an error that the context is not provided, this also affect the Qwik City contexts needed for useNavigation / useLocation.
For example:
useVisibleTask$(({ track })=>{// contentRef is a ref to div where we inserted the dynamic contentconstel=track(()=>contentRef.value)if(!el)returnel?.querySelectorAll('my-component')?.forEach((el)=>{// render the components and pass all the attributes from the HTML element to the componentconstattrs=Array.from(el.attributes).reduce((acc,attr)=>({ ...acc,[attr.name]: attr.value}),{})asMyComponentPropsrender(el,<MyComponent{...attrs}/>)})})
Would fail if MyComponent is using useNavigate().
After quite a long time trying to figure out how the context is resolved in Qwik I managed to get this to work by addind an option to the render method:
constrCtx=createRenderContext(doc,containerState);// CUSTOM: allow passing a context element to the render function// so that the rendered components have access to context providers// defined in the parent componentif(opts.contextEl){constctxEl=getElement(opts.contextEl);constctxElState=_getContainerState(ctxEl);constctx=findParentCtx(ctxEl,ctxElState);rCtx.$cmpCtx$=ctx;}// CUSTOM END
Finally changing the render call to be:
useVisibleTask$(({ track })=>{// contentRef is a ref to div where we inserted the dynamic contentconstel=track(()=>contentRef.value)if(!el)return// ADDED: get the Element for *this* qwik component since we want the inserted// component to inherit the contexts this one has access to then pass it as opts to renderconstcontextEl=el.parentElement?.parentElement||undefinedel?.querySelectorAll('my-component')?.forEach((el)=>{// render the components and pass all the attributes from the HTML element to the componentconstattrs=Array.from(el.attributes).reduce((acc,attr)=>({ ...acc,[attr.name]: attr.value}),{})asMyComponentPropsrender(el,<MyComponent{...attrs}/>,{ contextEl })})})
I am not sure if this is supposed to work, or work another way or maybe this hack is something that could useful to others.
Note: this was on Qwik 1.x
Which component is affected?
Qwik Runtime
Describe the bug
We are using the
render
method from@builder.io/qwik
to dynamically generate components on a page based on a CMS returned HTML.Unfortunately any attempt to call
useContext(...)
in those fail with an error that the context is not provided, this also affect the Qwik City contexts needed for useNavigation / useLocation.For example:
Would fail if
MyComponent
is usinguseNavigate()
.After quite a long time trying to figure out how the context is resolved in Qwik I managed to get this to work by addind an option to the render method:
then in the render method
Finally changing the render call to be:
I am not sure if this is supposed to work, or work another way or maybe this hack is something that could useful to others.
Note: this was on Qwik 1.x
Reproduction
https://stackblitz.com/edit/github-1r3nusmb?file=src%2Froutes%2Findex.tsx
Steps to reproduce
No response
System Info
Additional Information
No response
The text was updated successfully, but these errors were encountered: