Skip to content

Commit

Permalink
feat(functional): describe how to pass down attributes (#1405)
Browse files Browse the repository at this point in the history
* feat(functional): describe how to pass down attributes

* Tweak section about passing attributes to functional components
  • Loading branch information
posva authored and chrisvfritz committed Feb 2, 2018
1 parent 58f88a4 commit 56ce146
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/v2/guide/render-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,24 @@ Vue.component('smart-list', {
})
```

### Passing Attributes and Events to Child Elements/Components

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.

Functional components, however, require you to explicitly define this behavior:

```js
Vue.component('my-functional-button', {
functional: true,
render: function (createElement, context) {
// Transparently pass any attributes, event listeners, children, etc.
return createElement('button', context.data, context.children)
}
})
```

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.

### `slots()` vs `children`

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?
Expand Down

0 comments on commit 56ce146

Please sign in to comment.