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
tripolskypetr
changed the title
[🐞] How to pass the component as a component prop in Qwik?
How to pass the component as a component prop in Qwik?
Nov 6, 2024
In Qwik there's Slot component if you want to pass components as children, and there's also named Slot (the one with the name property, just like in Vue). This should solve your issue:
import{component$,Slot,PropsOf}from"@builder.io/qwik"constButton=component$(()=>(<button>{/* named slot has the name attribute */}<Slotname="icon"/>{/* And the default slot has none */}<Slot/></button>))exportconstDotIcon=component$<PropsOf<"svg">>(props=>(<svgxmlns="http://www.w3.org/2000/svg"viewBox="0 0 24 24"fill="currentColor"width="24"height="24"{...props}><circlecx="12"cy="12"r="5"/></svg>))exportdefaultcomponent$(()=>(<Button>{/* Notice the `q:slot` attribute - this is how you pass the name of the slot. No `q:slot` attribute means it'll project this component into default slot, which is the same as children prop in React */}<DotIconq:slot="icon"/>
Example button
</Button>))
Also, unlike with Vue, there's one catch with slots in Qwik: They don't support fallback (meaning you cannot render something as Slot child, so no default elements for slots)
And you should use inline components with caution: They're not the same as those created with component$ marker (read the Limitations section of the docs).
Which component is affected?
Qwik Runtime
Describe the bug
In react we can pass the component as a component property. For example
I notice the
PropFunction<(value: T) => void>
trick for callbacks but how do I pass the component as a prop exactly?Reproduction
https://codesandbox.io/p/sandbox/async-firefly-jgcsnp
Steps to reproduce
No response
System Info
Additional Information
No response
The text was updated successfully, but these errors were encountered: