diff --git a/src/guide/render-function.md b/src/guide/render-function.md index ab8539a2b4..47c5bf2774 100644 --- a/src/guide/render-function.md +++ b/src/guide/render-function.md @@ -62,9 +62,7 @@ const app = Vue.createApp({}) app.component('anchored-heading', { render() { - const { h } = Vue - - return h( + return Vue.h( 'h' + this.level, // tag name {}, // props/attributes this.$slots.default() // array of children @@ -164,6 +162,8 @@ h( ) ``` +If there are no props then the children can usually be passed as the second argument. In cases where that would be ambiguous, `null` can be passed as the second argument to keep the children as the third argument. + ## Complete Example With this knowledge, we can now finish the component we started: @@ -240,6 +240,47 @@ render() { } ``` +## Creating Component VNodes + +To create a VNode for a component, the first argument passed to `h` should be the component itself: + +```js +render() { + return Vue.h(ButtonCounter) +} +``` + +If we need to resolve a component by name then we can call `resolveComponent`: + +```js +render() { + const ButtonCounter = Vue.resolveComponent('ButtonCounter') + return Vue.h(ButtonCounter) +} +``` + +`resolveComponent` is the same function that templates use internally to resolve components by name. + +A `render` function will normally only need to use `resolveComponent` for components that are [registered globally](/guide/component-registration.html#global-registration). [Local component registration](/guide/component-registration.html#local-registration) can usually be skipped altogether. Consider the following example: + +```js +// We can simplify this +components: { + ButtonCounter +}, +render() { + return Vue.h(Vue.resolveComponent('ButtonCounter')) +} +``` + +Rather than registering a component by name and then looking it up we can use it directly instead: + +```js +render() { + return Vue.h(ButtonCounter) +} +``` + ## Replacing Template Features with Plain JavaScript ### `v-if` and `v-for` @@ -268,6 +309,8 @@ render() { } ``` +In a template it can be useful to use a `