Skip to content

Commit 56ce146

Browse files
posvachrisvfritz
authored andcommitted
feat(functional): describe how to pass down attributes (vuejs#1405)
* feat(functional): describe how to pass down attributes * Tweak section about passing attributes to functional components
1 parent 58f88a4 commit 56ce146

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/v2/guide/render-function.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,24 @@ Vue.component('smart-list', {
579579
})
580580
```
581581

582+
### Passing Attributes and Events to Child Elements/Components
583+
584+
On normal components, attributes not defined as props are automatically added to the root element of the component, replacing or [intelligently merging with](class-and-style.html) any existing attributes of the same name.
585+
586+
Functional components, however, require you to explicitly define this behavior:
587+
588+
```js
589+
Vue.component('my-functional-button', {
590+
functional: true,
591+
render: function (createElement, context) {
592+
// Transparently pass any attributes, event listeners, children, etc.
593+
return createElement('button', context.data, context.children)
594+
}
595+
})
596+
```
597+
598+
By passing `context.data` as the second argument to `createElement`, we are passing down any attributes or event listeners used on `my-functional-button`. It's so transparent, in fact, that events don't even require the `.native` modifier.
599+
582600
### `slots()` vs `children`
583601

584602
You may wonder why we need both `slots()` and `children`. Wouldn't `slots().default` be the same as `children`? In some cases, yes - but what if you have a functional component with the following children?

0 commit comments

Comments
 (0)