Use Vue.js Template Niceties in JSX Components

Joshua Bemenderfer

Certain people like JSX. Certain people think JSX is a way of life. I’m sure though that all parties agree that Vue.js’ templates do offer some convenient shorthand for common practices, even if they might idealistically disagree on whether or not they should exist. It is true, however, that JSX is more flexible. With flexibility though often comes verbosity. Particularly, Vue’s event modifiers and v-model don’t exist for JSX components.

Thankfully though, someone saw the need and created community plugins for both of these features, so now you can use JSX without much of the the extra cruft.

JSX v-model

While some people might take issue with v-model, others find it incredibly useful for reducing the amount of code needed to write and understand a single component. babel-plugin-jsx-v-model adds support for v-model in Vue JSX.

Installation

# Install through NPM...
npm install babel-plugin-jsx-v-model --save-dev
# ... or Yarn
yarn add babel-plugin-jsx-v-model -D

Now, add jsx-v-model to your plugins in your babel configuration.

.babelrc (partial)

...
  "plugins": ["jsx-v-model", "transform-vue-jsx"],
...

Usage

export default {
  data() {
    return {
      syncData: ''
    }
  },

  render: h => {
    return (
      <div class="component-wrapper">
        <h2>{this.syncData}</h2>
        <input type="text" v-model={this.syncData} />
      </div>
    )
  }
}

Now, it’s worth noting that this v-model implementation isn’t quite as robust as the Vue template one for now, but for most use-cases it should suffice. It sure beats having to write this every time:

export default {
  data() {
    return {
      syncData: ''
    }
  },

  render: h => {
    return (
      <div class="component-wrapper">
        <h2>{this.syncData}</h2>
        <input type="text" value={this.syncData} onInput={this.setSyncData}/>
      </div>
    )
  },

  methods: {
    setSyncData(evt) {
      this.syncData = evt.target.value
    }
  }
}

JSX Event modifiers

Event modifiers are another neat little trick Vue has that makes things just a little less verbose. (And also separates presentation logic from business logic.) babel-plugin-jsx-event-modifiers adds support for that too. (As an added bonus, this plugin works for React JSX as well.)

Now, it’s worth nothing that this doesn’t support the whole plethora of event modifiers available for Vue.js, nor does it add support for custom ones. It just implements the standard bubbling control and key modifiers.

  • :stop - Wraps the event with a function containing event.stopPropagation().
  • :prevent - Wraps the event with a function containing event.preventDefault().
  • :k[key code] Only fires the event if a matching key is pressed. (Note, this differs from the Vue implementation, which does not have the k prefix.) Only valid on key events.
  • :{key name} Only fires the event if a matching key is found (by name.)

    Valid Key Names:

    • esc
    • tab
    • enter
    • space
    • up
    • down
    • left
    • right
    • delete

Installation

# Install through NPM...
npm install babel-plugin-jsx-event-modifiers --save-dev
# ... or Yarn
yarn add babel-plugin-jsx-event-modifiers -D

Now, add jsx-event-modifiers to your plugins in your babel configuration.

.babelrc (partial)

...
  "plugins": ["jsx-event-modifiers", "transform-vue-jsx"],
...

Usage

export default {
  render: h => {
    return (
      <div class="component-wrapper">
        <button onClick:prevent={this.doSomething}>
          Do Something
        </button>

        <input type="text"
          placeholder="To do something, press enter."
          onKeyUp:enter={this.doSomething}
        />
      </div>
    )
  },

  methods: {
    doSomething() {
      ...
    }
  }
}

Which, again, is a significant improvement over the alternative:

export default {
  render: h => {
    return (
      <div class="component-wrapper">
        <button onClick:prevent={evt => {
          evt.preventDefault();
          this.doSomething();
        }}>
          Do Something
        </button>

        <input type="text"
          placeholder="To do something, press enter."
          onKeyUp:enter={evt => {
            if (evt.charCode === 13) {
                this.doSomething()
            }
          }}
        />
      </div>
    )
  },

  methods: {
    doSomething() {
      ...
    }
  }
}

So yeah, enjoy having some of the best parts of Vue templates in your JSX!

And I swear if any of you start grumbling about magic or non-javascriptyness

  Tweet It
✖ Clear

🕵 Search Results

🔎 Searching...