Skip to content

Commit 6b8bda5

Browse files
committed
Add checking-the-vnode-type.md
1 parent 05f54ca commit 6b8bda5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export default defineConfigWithTheme({
5151
}, {
5252
text: 'Replacing nodes',
5353
link: '/guide/replacing-nodes.html'
54+
}, {
55+
text: 'Checking the VNode type',
56+
link: '/guide/checking-the-vnode-type.html'
5457
}
5558
]
5659
}, {

docs/guide/checking-the-vnode-type.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Checking the VNode type
2+
3+
Vue has a built-in function, [`isVNode()`](https://vuejs.org/api/render-function.html#isvnode), for testing whether something is a VNode.
4+
5+
Most of the helpers provided by `vue-vnode-utils` require a callback that will be passed eligible VNodes. These will always be full VNodes, so it isn't necessary to use `isVNode()` within those callbacks.
6+
7+
It is common to want to handle different types of VNode differently inside the callback. For example, you might want to detect text VNodes. The type of a VNode can be checked using `getType()`:
8+
9+
```js
10+
const children = addProps(slotVNodes, (vnode) => {
11+
switch (getType(vnode)) {
12+
case 'component':
13+
// ...
14+
case 'element':
15+
// ...
16+
case 'text':
17+
// ...
18+
case 'comment':
19+
//
20+
}
21+
})
22+
```
23+
24+
In addition to the values shown above, `getType()` can also return `'static'`, `'fragment'` and `undefined`. Of those 3 values, only `'static'` can be encountered in the callbacks, but in practice that shouldn't happen if the VNode came from a slot.
25+
26+
The value passed to `getType()` doesn't have to be a fully instantiated VNode. It also supports being passed any value that can be converted to a VNode. So, for example, a string would be considered a text node. If you need to check that something really is a VNode then use `isVNode()`, but in practice this shouldn't be necessary for the values passed to callbacks by `vue-vnode-utils`.
27+
28+
There are also helpers for checking an individual type:
29+
30+
* [isComponent()](/api.html#iscomponent)
31+
* [isElement()](/api.html#iselement)
32+
* [isText()](/api.html#istext)
33+
* [isComment()](/api.html#iscomment)
34+
* [isFragment()](/api.html#isfragment)
35+
* [isStatic()](/api.html#isstatic)
36+
37+
For example:
38+
39+
```js
40+
const children = replaceChildren(slotVNodes, (vnode) => {
41+
if (isText(vnode) || isComment(vnode)) {
42+
// ...
43+
}
44+
})
45+
```
46+
47+
It should be noted that most of the callbacks can be pre-filtered by passing [`IterationOptions`](/api.html#iterationoptions), so it's only necessary to do explicit checks if the callback needs to handle multiple types.
48+
49+
Component VNodes can be split into two further categories using [isStatefulComponent()](/api.html#isstatefulcomponent) and [isFunctionalComponent()](/api.html#isfunctionalcomponent). The component itself is available via the VNode's `type` property. For an element VNode the `type` will be the tag name.
50+
51+
`getText()` can be used to read the text from a text node. As well as text VNodes, it also considers strings and numbers to be valid text nodes, consistent with `isText()`. If the passed value is not a valid text node then `undefined` will be returned.

0 commit comments

Comments
 (0)