Skip to content
Advertisement

Use Vue.js 3 fragments with render function

How should I use Vue 3 fragments with render functions? shouldn’t the following code work?

import { h } from 'vue'

render () {
  return [
    h('label', { htmlFor: 'username' }, this.label),
    h('input', { id: 'username' }),
  ]
},

Advertisement

Answer

Yes that syntax is correct for defining fragments in render functions :

import { h } from "vue";
export default {
  props: ["label", "errors"],

  render() {
    return [
      h("label", { htmlFor: "username" }, this.label),
      h("input", { id: "username" }),
      this.errors && h("span", { class: "red" }, this.errors)
    ];
  }
};

this is equivalent to :

<template>
 <label for="username"> {{this.label}}</label>
  <input id="username" />
   <span class="red" v-if="errors">{{errors}}</span>
</template>

LIVE DEMO

Advertisement