Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createElement() has been called outside of render function. when handleSuccessMessage function used #804

Closed
nishuonishuo opened this issue Aug 25, 2021 · 14 comments · Fixed by #920
Labels
contribution welcome The team would welcome a contribution from the community for this issue enhancement New feature or request pr welcome

Comments

@nishuonishuo
Copy link

image
image

@antfu
Copy link
Member

antfu commented Aug 25, 2021

Can you share a minimal reproduction? Thanks.

@antfu antfu added the needs reproduction We need a runnable reproduction from the OP label Aug 25, 2021
@nishuonishuo
Copy link
Author

https://codesandbox.io/s/sleepy-lederberg-50px2?file=/src/main.js
image
you can visit it and click for bug

@Ttou
Copy link

Ttou commented Sep 22, 2021

1.2.1 has same problem, 1.1.5 is ok

@k644606347
Copy link

em...same problem, v1.2.4

@github-actions
Copy link

github-actions bot commented Jan 9, 2022

Stale issue message

@Hyphon
Copy link

Hyphon commented Jan 11, 2022

v1.4.3 has same problem.

@MinatoHikari
Copy link
Contributor

MinatoHikari commented Jan 14, 2022

use setup: () => {} to avoid this warning.

@hisuwh
Copy link

hisuwh commented Mar 1, 2022

Any updates on this?

@MinatoHikari
Copy link
Contributor

MinatoHikari commented Apr 16, 2022

Let's check about createElement.ts

export const createElement = function createElement(...args: any) {
  const instance = getCurrentInstance()?.proxy
  if (!instance) {
    __DEV__ &&
      warn('`createElement()` has been called outside of render function.')
    if (!fallbackCreateElement) {
      fallbackCreateElement = defineComponentInstance(
        getVueConstructor()
      ).$createElement
    }

    return fallbackCreateElement.apply(fallbackCreateElement, args)
  }

  return instance.$createElement.apply(instance, args)
} as CreateElement

const instance = getCurrentInstance()?.proxy
This is the cause of warning.

Remember that we can't get instance out of setup with getCurrentInstance.
It returns null when you call it in click event.

The solution is calling h in setup function
https://codesandbox.io/s/lingering-currying-l03w6f?file=/src/components/HelloWorld.vue

@antfu
Copy link
Member

antfu commented Apr 17, 2022

PR welcome to bypass the warning in this case.

@antfu antfu added enhancement New feature or request contribution welcome The team would welcome a contribution from the community for this issue needs reproduction We need a runnable reproduction from the OP pr welcome and removed needs reproduction We need a runnable reproduction from the OP labels Apr 17, 2022
edwardnyc pushed a commit to edwardnyc/composition-api that referenced this issue Apr 23, 2022
@Dcx199302
Copy link

用于setup: () => {}避免此警告。

content: () => h('div', {}, [h('p', '使用函数返回正确执行')])
content: h('div', {}, [h('p', '直接调用报错上面提到的警告')]

@MinatoHikari
Copy link
Contributor

用于setup: () => {}避免此警告。

content: () => h('div', {}, [h('p', '使用函数返回正确执行')]) content: h('div', {}, [h('p', '直接调用报错上面提到的警告')]

这个是错的,需要给h绑定vm

@Dcx199302
Copy link

用于setup: () => {}避免此警告。

content: () => h('div', {}, [h('p', '使用函数返回正确执行')]) content: h('div', {}, [h('p', '直接调用报错上面提到的警告')]

这个是错的,需要给h绑定vm

该如何去做呢,该段代码我并没有(也不需要)在template写任何html代码

@MinatoHikari
Copy link
Contributor

用于setup: () => {}避免此警告。

content: () => h('div', {}, [h('p', '使用函数返回正确执行')]) content: h('div', {}, [h('p', '直接调用报错上面提到的警告')]

这个是错的,需要给h绑定vm

该如何去做呢,该段代码我并没有(也不需要)在template写任何html代码

const vm = getCurrentInstance()
const createElement = h.bind(vm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome The team would welcome a contribution from the community for this issue enhancement New feature or request pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants