A slot is a placeholder inside a child component's template that allows a parent component to inject custom content, making the child component flexible and reusable by letting the parent control parts of its layout.
ChildComponent.jsx
export default function ChildComponent({ children }) { return <div className="wrapper">{children}</div>;}ParentComponent.jsx
export default function ParentComponent() { return ( <ChildComponent> <p>Content for the child component's slot.</p> </ChildComponent> );}ChildComponent.vue
<template> <div class="wrapper"> <slot /> </div></template>ParentComponent.vue
<template> <ChildComponent> <p>Content for the child component's slot.</p> </ChildComponent></template>Slots should have fallback (default) content.
IconButton.jsx
export default function IconButton({ children }) { return ( <button className="btn"> {children || <span>π</span>} </button> );}Usage:
export default function() { return ( <div> <IconButton /> <IconButton> <span>β€οΈ</span> </IconButton> </div> );}IconButton.vue
<template> <button class="btn"> <slot> <span>π</span> </slot> </button></template>Usage:
<template> <div> <IconButton /> <IconButton> <span>β€οΈ</span> </IconButton> </div></template>