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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Leveraging Salesforce Using a Client Written In Vue.js
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker

Trending

  • Building a Windows App for a Resilient SSH SOCKS Proxy and Selective Browser Routing
  • OwlVigil Open-Sources Its Go SDK for Enterprise AI
  • Persisting Cookies Across JAX-WS Service Calls Using a Custom SOAP Handler
  • Java Virtual Threads Under Load: Three Production Limits

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Feb. 04, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. 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
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook