Skip to content

Commit d97f611

Browse files
committed
notes on render function usage
1 parent b8c42cd commit d97f611

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

active-rfcs/0000-slots-unification.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,59 @@ Slots unification involves two parts:
3333

3434
- In 2.x, all slots using `v-slot` syntax are already compiled as functions internally. `this.$scopedSlots` also proxies to normal slots and expose them as functions.
3535

36+
## Usage in Render Functions
37+
38+
Existing render function usage will remain supported. When passing children to a component, both VNodes and functions are supported:
39+
40+
``` js
41+
h(Comp, [
42+
h('div', this.msg)
43+
])
44+
45+
// equivalent:
46+
h(Comp, () => [
47+
h('div', this.msg)
48+
])
49+
```
50+
51+
Inside `Comp`, `this.$slots.default` will be a function in both cases and returns the same VNodes. However, the 2nd case will be more performant as `this.msg` will be registered as a dependency of the child component only.
52+
53+
Named slots usage has changed - instead of a special `slot` data property on the content nodes, pass them to the component's `slots`:
54+
55+
``` js
56+
// 2.x
57+
h(Comp, [
58+
h('div', { slot: 'foo' }, this.foo),
59+
h('div', { slot: 'bar' }, this.bar)
60+
])
61+
62+
// 3.0
63+
h(Comp, {
64+
slots: {
65+
foo: () => h('div', this.foo),
66+
bar: () => h('div', this.bar)
67+
}
68+
})
69+
```
70+
71+
### Further Manual Optimization
72+
73+
Note that when the parent component updates, `Comp` is always forced to also update because without the compile step Vue doesn't have enough information to tell whether `slots` may have changed.
74+
75+
The compiler can detect `v-slot` and compile content as functions, but in render functions this won't happen automatically. We may also be able to perform similar optimizations in our JSX babel plugin. But for users writing direct render functions, they need to do it manually in performance sensitive use cases.
76+
77+
Slots can be manually annotated so that Vue won't force the child to update when the parent updates:
78+
79+
``` js
80+
h(Comp, {
81+
slots: {
82+
$stable: true,
83+
foo: () => h('div', this.foo),
84+
bar: () => h('div', this.bar)
85+
}
86+
})
87+
```
88+
3689
# Adoption strategy
3790

3891
Majority of the change has already been shipped in 2.6. The only thing left is the removal of `this.$scopedSlots` from the API. In practice, the current `this.$scopedSlots` in 2.6 works exactly like `this.$slots` in 3.0, so the migration can happen in 2 steps:

0 commit comments

Comments
 (0)