Skip to content

Commit

Permalink
docs: add explanation for scoped slots with render functions (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbzx10299 committed Jan 17, 2024
1 parent 209f6a2 commit 0a3f144
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/guide/extras/render-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,41 @@ JSX equivalent:

Passing slots as functions allows them to be invoked lazily by the child component. This leads to the slot's dependencies being tracked by the child instead of the parent, which results in more accurate and efficient updates.

### Scoped Slots

To render a scoped slot in the parent component, a slot is passed to the child. Notice how the slot now has a parameter `text`. The slot will be called in the child component and the data from the child component will be passed up to the parent component.

```js
// parent component
export default {
setup() {
return () => h(MyComp, null, {
default: ({ text }) => h('p', text)
})
}
}
```

Remember to pass `null` so the slots will not be treated as props.

```js
// child component
export default {
setup(props, { slots }) {
const text = ref('hi')
return () => h('div', null, slots.default({ text: text.value }))
}
}
```

JSX equivalent:

```jsx
<MyComponent>{{
default: ({ text }) => <p>{ text }</p>
}}</MyComponent>
```

### Built-in Components {#built-in-components}

[Built-in components](/api/built-in-components) such as `<KeepAlive>`, `<Transition>`, `<TransitionGroup>`, `<Teleport>` and `<Suspense>` must be imported for use in render functions:
Expand Down

0 comments on commit 0a3f144

Please sign in to comment.