render-functions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRender Functions
渲染函数
Table of Contents
目录
Vue recommends for us to use templates (i.e. the syntax) to construct the markup of our Vue components. However, we're also given the opportunity to directly use something known as render functions to build the markup of our components as well.
<template></template>Vue, at build time, takes the templates we create for our components and compiles them to render functions. It's at these compiled render functions, where Vue builds a virtual representation of nodes that make up the virtual DOM.
When to Use
适用场景
- Use this when you need complex dynamic rendering logic that's hard to express with template directives
- This is helpful for component library development where flexibility and low-level control are needed
- 当你需要实现复杂的动态渲染逻辑,而模板指令难以表达时,使用渲染函数
- 这在组件库开发中很有帮助,因为这类场景需要灵活性和底层控制能力
When NOT to Use
不适用场景
- When templates handle the use case — templates are more readable and benefit from compile-time optimizations
- For standard component markup where ,
v-if, and slots cover the rendering needsv-for - When the team is unfamiliar with / JSX and the maintenance cost outweighs the flexibility gain
h()
- 当模板可以满足需求时——模板可读性更强,并且能受益于编译时优化
- 对于标准组件标记,、
v-if和插槽已经能满足渲染需求的情况v-for - 当团队不熟悉/JSX,维护成本超过灵活性带来的收益时
h()
Instructions
使用指南
- Use the function with three arguments: tag/component, props/attributes, and children
h() - Use JSX with as a more readable alternative to raw
@vue/babel-plugin-jsxcallsh() - Prefer Vue templates for most application code — render functions are for advanced cases
- Remember that Vue JSX uses (not
class) and single curly bracesclassName{}
- 使用带有三个参数的函数:标签/组件、属性/特性、子节点
h() - 使用搭配的JSX作为原生
@vue/babel-plugin-jsx调用的更易读替代方案h() - 大多数应用代码优先选择Vue模板——渲染函数适用于高级场景
- 记住Vue JSX使用(而非
class),并且用单大括号className嵌入变量{}
Details
详细说明
By using render functions, we skip the compile step that Vue takes to compile our templates, and are able to construct our component templates with the help of programmatic JavaScript.
通过使用渲染函数,我们跳过了Vue编译模板的步骤,能够借助程序化的JavaScript来构建组件模板。
But why?
为什么要使用?
Render functions come into play when we require a higher level of customization and flexibility that's not easily achievable with the standard template syntax. In a nutshell, you may prefer to use render functions:
- When you need to dynamically render components or elements based on complex logic that can be cumbersome to express within a template.
- You want to have a direct hand on the Virtual DOM for advanced manipulations.
- You want to use JSX for building the template of your components.
Outside of these unique cases, Vue's template syntax should remain the go-to method for constructing component markup.
当我们需要更高程度的自定义和灵活性,而标准模板语法无法轻松实现时,渲染函数就派上用场了。简而言之,你可能更倾向于使用渲染函数的场景包括:
- 当你需要基于复杂逻辑动态渲染组件或元素,而这些逻辑在模板中表达起来很繁琐时
- 你想要直接操作虚拟DOM以进行高级处理时
- 你想要使用JSX来构建组件模板时
除了这些特殊情况,Vue的模板语法应该仍是构建组件标记的首选方式。
Render functions
渲染函数
Assume we had the following component that contains a element encompassing a element. The text content of the element simply displays the value of a prop.
<div><header><header>messagehtml
<template>
<div class="render-card">
<header class="card-header card-header-title">{{ message }}</header>
</div>
</template>
<script setup>
const { message } = defineProps(["message"]);
</script>We'll recreate the markup of the component step by step with the help of the render function — i.e. the function.
h()hh()The function takes three arguments of its own:
h()- An HTML tag name or a component definition.
- The props/attributes to be passed onto the element (event listeners, class attributes, etc.).
- Child nodes of the parent node.
Here's the full render function equivalent:
html
<template>
<render />
</template>
<script setup>
import { h } from "vue";
const { message } = defineProps(["message"]);
const render = () => {
return h(
"div",
{
class: "render-card",
},
[
h(
"header",
{
class: "card-header card-header-title",
},
message
),
]
);
};
</script>We can now render the above component in the parent instance and pass a value of to the prop.
App.vue"Hello World!"messagehtml
<template>
<RenderComponent message="Hello world!" />
</template>
<script setup>
import RenderComponent from "./components/RenderComponent.vue";
</script>The component constructed with a render function produces the exact same output to its template equivalent.
假设我们有如下组件,它包含一个元素,里面嵌套了一个元素。元素的文本内容显示属性的值。
<div><header><header>messagehtml
<template>
<div class="render-card">
<header class="card-header card-header-title">{{ message }}</header>
</div>
</template>
<script setup>
const { message } = defineProps(["message"]);
</script>我们将借助渲染函数——即函数——逐步重建该组件的标记。
h()hh()h()- HTML标签名或组件定义
- 要传递给元素的属性/特性(事件监听器、class属性等)
- 父节点的子节点
以下是对应的完整渲染函数实现:
html
<template>
<render />
</template>
<script setup>
import { h } from "vue";
const { message } = defineProps(["message"]);
const render = () => {
return h(
"div",
{
class: "render-card",
},
[
h(
"header",
{
class: "card-header card-header-title",
},
message
),
]
);
};
</script>现在我们可以在父组件中渲染上述组件,并给属性传递值。
App.vuemessage"Hello World!"html
<template>
<RenderComponent message="Hello world!" />
</template>
<script setup>
import RenderComponent from "./components/RenderComponent.vue";
</script>使用渲染函数构建的组件,其输出与使用模板构建的完全相同。
JSX
JSX
JSX is a syntax extension that allows us to write HTML-like code within JavaScript. With Vue, JSX can be used as an alternative to the function to construct render functions.
h()Vue's JSX support isn't built in like in React. We need to use a specific Babel plugin — — to have our JSX code transformed into the appropriate function calls.
@vue/babel-plugin-jsxh()Here's the same render function component we've built before but now recreated with JSX:
jsx
<script setup>
const { message } = defineProps(["message"]);
const render = () => {
return (
<div class="render-card">
<header class="card-header card-header-title">{message}</header>
</div>
);
};
</script>Since JSX is closer to JavaScript than to HTML, Vue JSX components use instead of and variables are embedded with single curly braces instead of double curly braces .
classclassName{}{{ }}JSX是一种语法扩展,允许我们在JavaScript中编写类HTML的代码。在Vue中,JSX可以作为函数的替代方案来构建渲染函数。
h()Vue的JSX支持不像React那样内置。我们需要使用特定的Babel插件————来将JSX代码转换为相应的函数调用。
@vue/babel-plugin-jsxh()以下是我们之前用渲染函数构建的组件,现在改用JSX重建:
jsx
<script setup>
const { message } = defineProps(["message"]);
const render = () => {
return (
<div class="render-card">
<header class="card-header card-header-title">{message}</header>
</div>
);
};
</script>由于JSX更贴近JavaScript而非HTML,Vue JSX组件使用而非,并且用单大括号嵌入变量,而非双大括号。
classclassName{}{{ }}When to use render functions
何时使用渲染函数
- If your component has complex, conditional rendering logic that is hard to express with template directives, render functions (with or without JSX) can provide a cleaner solution.
- If you want more direct control over the virtual DOM.
- In library/component-kit development where flexibility and low-level control are needed.
For most typical application development, Vue templates offer the right level of expressiveness and readability. Render functions and JSX are powerful tools to reach for when templates aren't enough.
- 如果你的组件有复杂的条件渲染逻辑,用模板指令难以清晰表达,渲染函数(带或不带JSX)可以提供更简洁的解决方案
- 如果你想要更直接地控制虚拟DOM
- 在库/组件套件开发中,这类场景需要灵活性和底层控制能力
对于大多数典型的应用开发,Vue模板提供了恰到好处的表达能力和可读性。当模板不足以满足需求时,渲染函数和JSX是强大的备选工具。