Skip to content

Expand the explanation about using VNodes with <component> #1310

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

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/api/built-in-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { KeepAlive, Teleport, Transition, TransitionGroup } from 'vue'

- **Props:**

- `is` - `string | Component`
- `is` - `string | Component | VNode`

- **Usage:**

Expand All @@ -41,6 +41,8 @@ import { KeepAlive, Teleport, Transition, TransitionGroup } from 'vue'
<component :is="href ? 'a' : 'span'"></component>
```

- **Usage with built-in components:**

The built-in components `KeepAlive`, `Transition`, `TransitionGroup`, and `Teleport` can all be passed to `is`, but you must register them if you want to pass them by name. For example:

```js
Expand All @@ -62,15 +64,15 @@ import { KeepAlive, Teleport, Transition, TransitionGroup } from 'vue'

Registration is not required if you pass the component itself to `is` rather than its name.

- **key:**
- **Usage with VNodes:**

When using <component :is="vnode"> and passing vnode of the same type, you need to provide keys:
```html
<component :is="current" :key="selected" />
```
In advanced use cases, it can sometimes be useful to render an existing VNode via a template. Using a `<component>` makes this possible, but it should be seen as an escape hatch, used to avoid rewriting the entire template as a `render` function.

```html
<component :is="vnode" :key="aSuitableKey" />
```

Otherwise, you are passing two compiled vnodes of the same type to the renderer. Because they are compiled as completely static, they will not be updated at all.
A caveat of mixing VNodes and templates in this way is that you need to provide a suitable `key` attribute. The VNode will be considered static, so any updates will be ignored unless the `key` changes. The `key` can be on the VNode or the `<component>` tag, but either way it must change every time you want the VNode to re-render. This caveat doesn't apply if the nodes have different types, e.g. changing a `span` to a `div`.

- **See also:** [Dynamic Components](../guide/component-dynamic-async.html)

Expand Down