Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I should use render: h => h(App)?

Tags:

vue.js

By the docs I do not fully understand when I should use render: h => h(App) function.

For example I have very simple Vue app:

import Vue from 'vue'
import App from './App.vue'

new Vue({
    el: '#app',
    components: { App }
})

What is the case when I need add to code: render: h => h(App)?

like image 340
Dmitry Bubnenkov Avatar asked Mar 01 '17 14:03

Dmitry Bubnenkov


People also ask

What is H in Vue render?

Vue provides an h() function for creating vnodes: js import { h } from 'vue' const vnode = h( 'div', // type { id: 'foo', class: 'bar' }, // props [ /* children */ ] ) h() is short for hyperscript - which means "JavaScript that produces HTML (hypertext markup language)".

Should I use JSX in Vue?

Love it or hate it, JSX is a popular extension to JavaScript that allows XML tokens in your scripts. If you want to create templates in your script files and you find Vue's render() function to be difficult to work with, JSX may be just what you need.

What is VNode Vue?

A VNode is an "element" in Vue's Virtual DOM, which is what Vue uses to create the real HTML DOM that's rendered on the page.

What is a functional component in Vue?

Functional components defined as a Single-File Component in a *. vue file also receives proper template compilation, Scoped CSS and hot-reloading support. To denote a template that should be compiled as a functional component, add the functional attribute to the template block.


1 Answers

In the example you posted, App.vue represents the main app wrapper - all related .vue component files would be imported there.

So in your Vue instance you defined the components object, and added the App component, but how would you exactly mount that component to the instance and display it?

A couple of things to keep in mind:

  • This is not a .vue file, so you don't have a template tag option. This is a pure .js file
  • You can't put <App></App> into the template property because you are using runtime only build so the template option is not available

One option is using render functions. It's what Vue.js does under the hood. It takes your template and then template compiler converts it to render functions.

Alternatively, instead of a render function, you can the use spread operator:

import Vue from 'vue'
import App from './App.vue'

new Vue({
    el: '#app',
    ...App
})

From the docs:

Vue recommends using templates to build your HTML in the vast majority of cases. There are situations however, where you really need the full programmatic power of JavaScript. That’s where you can use the render function

like image 163
Belmin Bedak Avatar answered Oct 19 '22 11:10

Belmin Bedak