Skip to content

Commit 7c1c248

Browse files
committed
docs: note about createElement workaround
1 parent e0128e3 commit 7c1c248

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,37 @@ app2.component('Bar', Bar) // equivalent to Vue.use('Bar', Bar)
401401

402402
</details>
403403

404+
### `createElement` / `h`
405+
406+
<summary>
407+
⚠️ <code>createElement</code> / <code>h</code> workaround
408+
</summary>
409+
410+
<br>
411+
412+
`createElement` / `h` in Vue 2 is only accessable in `render()` function. To use it outside of `render()`, you can explicitly bind a component instance to it.
413+
414+
> :warning: **Warning**: This ability is provided as a workaround Vue 2, it's not part of the Vue 3 API.
415+
416+
```jsx
417+
import { h as _h } from '@vue/composition-api'
418+
419+
export default {
420+
setup() {
421+
const vm = getCurrentInstance()
422+
const h = _h.bind(vm)
423+
424+
return () =>
425+
h('div', {
426+
ref: 'root',
427+
})
428+
},
429+
}
430+
```
431+
432+
</details>
433+
434+
404435
### `shallowReadonly`
405436

406437
<details>

src/apis/createElement.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { VNode, VNodeChildren, VNodeData } from 'vue/types/vnode'
1111

1212
export interface H extends CreateElement {
1313
(
14-
this: ComponentInternalInstance | null,
14+
this: ComponentInternalInstance | null | undefined,
1515
tag?:
1616
| string
1717
| Component<any, any, any, any>
@@ -20,7 +20,7 @@ export interface H extends CreateElement {
2020
children?: VNodeChildren
2121
): VNode
2222
(
23-
this: ComponentInternalInstance | null,
23+
this: ComponentInternalInstance | null | undefined,
2424
tag?:
2525
| string
2626
| Component<any, any, any, any>
@@ -33,8 +33,8 @@ export interface H extends CreateElement {
3333

3434
let fallbackCreateElement: CreateElement
3535

36-
export const createElement = function createElement(this, ...args: any) {
37-
const instance = this ? this.proxy : getCurrentInstance()?.proxy
36+
export const createElement: H = function createElement(this, ...args: any) {
37+
const instance = this?.proxy || getCurrentInstance()?.proxy
3838
if (!instance) {
3939
__DEV__ &&
4040
warn('`createElement()` has been called outside of render function.')
@@ -48,4 +48,4 @@ export const createElement = function createElement(this, ...args: any) {
4848
}
4949

5050
return instance.$createElement.apply(instance, args)
51-
} as H
51+
}

0 commit comments

Comments
 (0)