Skip to content

Commit f0906e3

Browse files
committed
Add replacing-nodes.md
1 parent c1f35f7 commit f0906e3

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export default defineConfigWithTheme({
4848
}, {
4949
text: 'Inserting new nodes',
5050
link: '/guide/inserting-new-nodes.html'
51+
}, {
52+
text: 'Replacing nodes',
53+
link: '/guide/replacing-nodes.html'
5154
}
5255
]
5356
}, {

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ A new array will be returned and the passed array and its contents should be lef
373373
### See also
374374

375375
* [Example - Wrap children](/examples.html#wrap-children)
376+
* [Guide - Replacing nodes](/guide/replacing-nodes.html)
376377

377378
## someChild()
378379

docs/guide/replacing-nodes.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Replacing nodes
2+
3+
`replaceChildren()` can be used to add, remove or replace VNodes.
4+
5+
```js
6+
import { cloneVNode, h } from 'vue'
7+
import { replaceChildren } from '@skirtle/vue-vnode-utils'
8+
9+
// Inside a render function
10+
const children = slots.default?.() || []
11+
12+
const newChildren = replaceChildren(children, (vnode) => {
13+
// If the child is already a <li>, ensure it has the 'list-item' class
14+
if (vnode.type === 'li') {
15+
return cloneVNode(vnode, { class: 'list-item' })
16+
}
17+
18+
// If the child isn't already a <li>, wrap it in one
19+
return h('li', { class: 'list-item' }, [vnode])
20+
})
21+
```
22+
23+
See it on the SFC Playground: [Composition API](https://sfc.vuejs.org/#eNp1VMlu2zAQ/ZWBerAMaEuLXlw7XXIq0PTYSxQUjEVZTCiSICkvcPzvnaEkJ0pqwwbI4Zv3ZvUx+m5Mtu14tIiWbm2F8eC478x1qURrtPVwBMvrBNa6NZ3nFZygtrqFGTrNzqCbRsjqBiFaceUHRJZPzaSDLqVaa+U8MnYIXRF9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gmRGdsT29Hpho49w2k9nA/gJy9qzfvzUgoSINvygaCvfd0Qbj+HAyjoXMKEjqQYbu4KJG2rcdYyul9G0zs83+v7HVSsCZ9NrWgWXOXS4YLEptKmHvXb2qlXIjYmiSJTZHYInk1Y63VEp1BTEUTFQ26q4EnXW8M1Eace9avn+GFehEbJLTrYKCpe0KKQTuhI3KrmZU2tqaJr+CgXBs+dJ7I535xKEziiiw3XgeYq59r25/pzzqAXTxrHhKp4uRcfLdUBMkmAf38pXx9K6Ia96tdoAmlO3cm44yTxCXatEh+tYNGqDEVNc7//TXWa//eJ2MgaDXRuOVLMamUQslFl1V9T1RzD95CW6PUVHh4t+4hWfDultTpPdD5y+TB36S1gddsPT4pRBL8KwJL3iiAMMKk5y/fA7Jzf+sCVaTWnGYeSGQedSco9sEM/o9IxY2wmtOPFIBDaXYPVamWRs2HpLWJY+mO5e7wtguNJTqg7WnwbRc28lqya4UW+W80amy3tJcn0FINwObcJXZPz4cGl8+h0rTqJ0u8Y3EoKfldsglz8GxncS/tXFE7U/fufak3zmsBNswNTS5HDpziOv9mrium1UAHWTQJfm93g2wVbkWOZwOc4HpwNy3Oh1iOq13Z6XvcGG48eGA==)
24+
25+
As shown in the example above, `replaceChildren()` can be combined with `cloneVNode()` to recreate the functionality of [`addProps()`](/guide/adding-props.html). It can also be used instead of [`betweenChildren()`](/guide/inserting-new-nodes.html) to insert nodes.
26+
27+
The callback function can return either a single VNode or an array of children. The returned nodes are inserted into the tree to replace the node passed to the callback. The original node can be included in the returned array. If the callback returns `null` or `undefined` then the tree will be left unchanged. An empty array can be used to remove a node from the tree.
28+
29+
As with the other helpers, fragment VNodes will be skipped and their children will be walked instead. The fragments will be cloned when replacing child nodes but the overall structure of the fragments will be preserved.
30+
31+
If the callback returns an array it will not be treated as a fragment, the individual children will be added in place of the current node. Any arrays nested within the returned array will be treated as fragments, just as they would when passing children to `h()`. When making changes to the tree structure it is important to consider the impact of fragments and `key` values to ensure that the VNodes get paired up correctly across re-renders.
32+
33+
`replaceChildren()` takes an optional third argument specifying [iteration options](/api.html#iterationoptions), much like with the [iterators](/guide/iterators.html). Unlike those iterators, the default value for `replaceChildren()` is `SKIP_COMMENTS`.

0 commit comments

Comments
 (0)