Using JSX in Vue 3: Benefits, Comparison with Template Syntax, and Configuration Guide
This article examines how JSX can be used in Vue 3, compares it with Vue's native template syntax, provides step‑by‑step configuration instructions, and discusses the advantages, disadvantages, and suitable scenarios for adopting JSX in modern frontend development.
In frontend development, Vue has long been favored for its simplicity and efficiency, but the growing popularity of React has sparked interest in JSX (JavaScript XML) as a declarative programming style. This article explores the application of JSX in Vue 3 and evaluates whether JSX could become the future of Vue 3 development.
Vue 3 and JSX: Love‑Hate Relationship
Before diving in, we briefly review Vue's native template syntax and JSX.
Vue 3 Template Syntax
Vue 3 template syntax is a declarative HTML‑like language that binds data to the view using interpolation, directives, and event bindings, which makes it easy to learn and use.
<template>
<div>
<h1>
{{ title }}
</h1>
<p v-if="showText">
{{ text }}
</p>
<ul>
<li v-for="item in list" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Vue3 Template Syntax',
text: 'This is a demo text.',
showText: true,
list: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
],
};
},
};
</script>Vue's template syntax uses double curly braces {{ }} for interpolation and directives such as v-if and v-for for conditional rendering and list iteration.
What Is JSX?
JSX is a JavaScript syntax extension that allows XML‑like structures to be written directly inside JavaScript code. React popularized JSX, and over time it has been adopted by many other frameworks and libraries.
How to Configure JSX in Vue 3
Vue provides the create‑vue scaffolding tool. After installing Node 16+ you can run:
npm init vue@latestDuring the interactive setup, choose Yes for "Add JSX Support?". The tool will install the necessary Babel plugin automatically.
For an existing Vue project, install the JSX Babel plugin:
npm install --save-dev @vue/babel-plugin-jsxThen add the plugin to vite.config.ts (or the corresponding Babel config). After configuration, JSX can be used alongside the regular template syntax.
Template Syntax vs. JSX
The following sections compare concrete code examples.
1. Data Interpolation
Template:
<template>
<p>{{ message }}</p>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const message = ref('Hello, JSX!');
return { message };
}
};
</script>JSX:
import { defineComponent, ref } from 'vue';
const DynamicData = defineComponent({
setup() {
const message = ref('Hello, JSX!');
return { message };
},
render() {
return
{this.message}
;
}
});2. Conditional Rendering
Template uses v-if :
<template>
<div>
<p v-if="showContent">Content is visible.</p>
<p v-else>Content is hidden.</p>
<button @click="toggleContent">Toggle</button>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const showContent = ref(true);
function toggleContent() { showContent.value = !showContent.value; }
return { showContent, toggleContent };
}
};
</script>JSX uses JavaScript expressions:
import { defineComponent, ref } from 'vue';
const ConditionalRender = defineComponent({
setup() {
const showContent = ref(true);
return { showContent };
},
render() {
return (
{this.showContent ?
Content is visible.
:
Content is hidden.
}
(this.showContent = !this.showContent)}>Toggle
);
}
});3. List Rendering
Template with v-for :
<template>
<ul>
<li v-for="item in items" :key="item">{{ item }}</li>
</ul>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const items = ref(['Apple', 'Banana', 'Orange']);
return { items };
}
};
</script>JSX with map :
import { defineComponent, ref } from 'vue';
const ListRendering = defineComponent({
setup() {
const items = ref(['Apple', 'Banana', 'Orange']);
return { items };
},
render() {
return (
{this.items.map(item => (
{item}
))}
);
}
});Advantages and Disadvantages of Template Syntax vs. JSX
Syntax Conciseness : Vue templates use plain HTML, which is easy for designers, but complex logic can become verbose. JSX leverages JavaScript, making conditional rendering and loops more compact.
Component Development : Both support components, but JSX allows components to be defined directly in JavaScript, offering more flexibility.
Type‑Checking & IDE Support : JSX integrates seamlessly with TypeScript and modern IDEs, providing better autocomplete and navigation compared to Vue's template directives.
Extensibility : JSX can be extended with regular JavaScript functions, whereas Vue's template language is fixed.
Ecosystem Interoperability : JSX benefits from the massive React ecosystem, and many libraries support JSX across frameworks, making migration between stacks easier.
Suitable Scenarios
When to Use Vue Template Syntax
Rapid prototyping or small projects where speed matters.
Teams with strong HTML/CSS backgrounds.
When to Use JSX
Complex interactive interfaces requiring advanced conditional logic.
Projects that already rely on the React ecosystem or where the team is familiar with JSX.
Conclusion
From development experience, ecosystem richness, and future trends, JSX is poised to become a major option for Vue 3 frontend development. It offers clearer component structures, powerful JavaScript expression capabilities, and better cross‑stack compatibility. While Vue 2's template syntax still has its place, adopting JSX in Vue 3 expands possibilities for building sophisticated user interfaces while retaining Vue's performance benefits.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.