|
| 1 | +# Render Function API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This change will not affect `<template>` users. |
| 6 | + |
| 7 | +Here is a quick summary of what has changed: |
| 8 | + |
| 9 | +- `h` is now globally imported instead of passed to render functions as an arguments |
| 10 | +- render function arguments changed to be more consistent between stateful and functional components |
| 11 | +- VNodes now have a flat props structure |
| 12 | + |
| 13 | +For more information, read on! |
| 14 | + |
| 15 | +## Render Function Argument |
| 16 | + |
| 17 | +### Previous Syntax |
| 18 | + |
| 19 | +In 2.x, the `render` function would automatically receive the `h` function (which is a conventional alias for `createElement`) as an argument: |
| 20 | + |
| 21 | +```js |
| 22 | +// Vue 2 Render Function Example |
| 23 | +export default { |
| 24 | + render(h) { |
| 25 | + return h('div') |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Current Syntax |
| 31 | + |
| 32 | +In 3.x, `h` is now globally imported instead of being automatically passed as an argument. |
| 33 | + |
| 34 | +```js |
| 35 | +// Vue 3 Render Function Example |
| 36 | +import { h } from 'vue' |
| 37 | + |
| 38 | +export default { |
| 39 | + render() { |
| 40 | + return h('div') |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Render Function Signature Change |
| 46 | + |
| 47 | +### Previous Syntax |
| 48 | + |
| 49 | +In 2.x, the `render` function automatically received arguments such as `h`. |
| 50 | + |
| 51 | +```js |
| 52 | +// Vue 2 Render Function Example |
| 53 | +export default { |
| 54 | + render(h) { |
| 55 | + return h('div') |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Current Syntax |
| 61 | + |
| 62 | +In 3.x, since the `render` function no longer receives any arguments, it will primarily be used inside of the `setup()` function. This has the added benefit of gaining access to reactive state and functions declared in scope, as well as the arguments passed to `setup()`. |
| 63 | + |
| 64 | +```js |
| 65 | +import { h, reactive } from 'vue' |
| 66 | + |
| 67 | +export default { |
| 68 | + setup(props, { slots, attrs, emit }) { |
| 69 | + const state = reactive({ |
| 70 | + count: 0 |
| 71 | + }) |
| 72 | + |
| 73 | + function increment() { |
| 74 | + state.count++ |
| 75 | + } |
| 76 | + |
| 77 | + // return the render function |
| 78 | + return () => h( |
| 79 | + 'div', |
| 80 | + { |
| 81 | + onClick: increment |
| 82 | + }, |
| 83 | + state.count |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +For more information on how `setup()` works, see our [Composition API Guide](/guide/composition-api-introduction.html). |
| 90 | + |
| 91 | +## VNode Props Format |
| 92 | + |
| 93 | +### Previous Syntax |
| 94 | + |
| 95 | +In 2.x, `domProps` contained a nested list within the VNode props: |
| 96 | + |
| 97 | +```js |
| 98 | +// 2.x |
| 99 | +{ |
| 100 | + class: ['button', 'is-outlined'], |
| 101 | + style: { color: '#34495E' }, |
| 102 | + attrs: { id: 'submit' }, |
| 103 | + domProps: { innerHTML: '' }, |
| 104 | + on: { click: submitForm }, |
| 105 | + key: 'submit-button' |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Current Syntax |
| 110 | + |
| 111 | +In 3.x, the entire VNode props structure is flattened. Using the example from above, here is what it would look like now. |
| 112 | + |
| 113 | +```js |
| 114 | +// 3.x Syntax |
| 115 | +{ |
| 116 | + class: ['button', 'is-outlined'], |
| 117 | + style: { color: '#34495E' }, |
| 118 | + attrs: { id: 'submit' }, |
| 119 | + innerHTML: '', |
| 120 | + on: { click: submitForm }, |
| 121 | + key: 'submit-button' |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## How to Migrate |
| 126 | + |
| 127 | +### Library Authors |
| 128 | + |
| 129 | +`h` being globally imported means that any library that contains Vue components will include `import { h } from 'vue'` somewhere. As a result, this creates a bit of overhead since it requires library authors to properly configure the externalization of Vue in their build setup: |
| 130 | + |
| 131 | +- Vue should not be bundled into the library |
| 132 | +- For module builds, the import should be left alone and be handled by the end user bundler |
| 133 | +- For UMD / browser builds, it should try the global Vue.h first and fallback to require calls |
| 134 | + |
| 135 | +## Next Steps |
| 136 | + |
| 137 | +See [Render Function Guide](/guide/render-function) for more detailed documentation! |
0 commit comments