DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Leveraging Salesforce Using a Client Written In Vue.js
  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • Validation Forms in Vue.js Apps Using Vuelidate library

Trending

  • Java Is Greener on Arm
  • How to Read JSON Files in Java Using the Google Gson Library
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Reactive Kafka With Spring Boot
  1. DZone
  2. Coding
  3. JavaScript
  4. Using JSX With Vue.js

Using JSX With Vue.js

While some people are still not sold on the use of JSX in their JavaScript, it has several benefits which we'll explore in this article.

By 
Anthony Gore user avatar
Anthony Gore
DZone Core CORE ·
Updated Feb. 04, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
12.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

To demonstrate, here's a render function without JSX:

JavaScript
 




x


 
1
render (h) {
2
  return h(
3
    'div',
4
    { attrs: { id: 'my-id' },
5
    [ this.msg ]
6
  );
7
}



And with JSX:

JavaScript
 




xxxxxxxxxx
1


 
1
render (h) {
2
  return (
3
    <div id='my-id'>,
4
      { this.msg } 
5
    </div>
6
  );
7
}



If nothing else, it's much easier to read!

Of course, JSX cannot be interpreted by the browser. It must be first transpiled into standard JavaScript, similar to how SASS first needs to be transpiled to CSS. More on this later.

You may also like:  React.js vs. Vue.js: A Comparison of Popular Frameworks.

Why Use JSX With Vue?

There are many ways to specify a template in Vue:

  1. Using an HTML file.
  2. Using a template property in your instance/component constructor.
  3. Using <template> tags in single file components.
  4. Using a render function.

If you go with option 4, you have to create your nodes in a very unfamiliar way, i.e. using the createElement JavaScript syntax.

JSX allows you use a render function and still have the look and feel of HTML. Some people find this much easier. Others find it dirty to mix HTML in with their JS.

Have a look at this example and you can decide if you like it or not.

JSX Example

We're now going to create a dead-simple app that displays a span with the text content “Show the message.” When you click the span it will trigger an alert.

Firstly, let’s use Vue in the normal way with separate JS and HTML files:

script.js

JavaScript
 




xxxxxxxxxx
1
11


 
1
new Vue({
2
  el: '#app',
3
  data: {
4
    msg: 'Show the message'
5
  },
6
  methods: {
7
    hello () {
8
      alert('Here is the message')
9
    }
10
  }
11
});



index.html

HTML
 




xxxxxxxxxx
1


 
1
<div id="app">
2
  <span class="my-class" v-on:click="hello">
3
    {{ msg }}
4
  </span>
5
</div>



Incorporating a Render Function

The following code does exactly the same thing as the above code, the only difference is that rather than using a template we will use a render function to create our template:

script.js

JavaScript
 




xxxxxxxxxx
1
23


 
1
new Vue({
2
  el: '#app',
3
  data: {
4
    msg: 'Show the message'
5
  },
6
  methods: {
7
    hello () {
8
      alert('Here is the message')
9
    }
10
  },
11
  render (createElement) {
12
    return createElement(
13
      'span',
14
      {
15
        class: { 'my-class': true },
16
        on: {
17
          click: this.hello
18
        }
19
      },
20
      [ this.msg ]
21
    );
22
  },
23
});



index.html

HTML
 




xxxxxxxxxx
1


 
1
<div id="app">
2
  <!--span will render here-->
3
</div>



Incorporating JSX

The render function is a little hard to read, right? And that's just for one span, imagine using it for a more complex component!

Let's use JSX now:

script.js

JavaScript
 




xxxxxxxxxx
1
18


 
1
new Vue({
2
  el: '#app',
3
  data: {
4
    msg: 'Show the message.'
5
  },
6
  methods: {
7
    hello () {
8
      alert('This is the message.')
9
    }
10
  },
11
  render(h) {
12
    return (
13
      <span class={{ 'my-class': true }} on-click={ this.hello } >
14
        { this.msg }
15
      </span>
16
    )
17
  }
18
});



(index.html same as above)

Transpiling JSX

JSX is just for development and will be transpiled away long before runtime. So we only need to consider JSX in terms of how it benefits our development flow (or not).

To transpile your JSX, you can use the babel-plugin-transform-vue-jsx module which is a plugin for Babel and Webpack. Simply add it to your Webpack config:

JavaScript
 




xxxxxxxxxx
1


 
1
{
2
  test: /\.js$/,
3
  exclude: [/node_modules/],
4
  use: [{
5
    loader: 'babel-loader',
6
    options: { presets: ['es2015'], plugins: ['transform-vue-jsx'] }
7
  }]
8
}



Now, when you do a webpack build, your JSX will be transpiled into standard JS.

Vue.js JavaScript

Published at DZone with permission of Anthony Gore, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Leveraging Salesforce Using a Client Written In Vue.js
  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • Validation Forms in Vue.js Apps Using Vuelidate library

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: