DEV Community

Ryan John
Ryan John

Posted on • Originally published at vureact.top

How does VuReact implement Vue v-on in React

VuReact is a compiler toolchain for migrating from Vue to React — and for writing React with Vue 3 syntax. In this article, we dive straight into the core: how Vue's common v-on/@ directive is compiled into React code by VuReact.

Before We Start

To keep the examples easy to read, this article follows two simple conventions:

  1. All Vue and React snippets focus on core logic only, with full component wrappers and unrelated configuration omitted.
  2. The discussion assumes you are already familiar with Vue 3's v-on directive usage.

Compilation Mapping

v-on / @: Basic event binding

v-on (shorthand @) is Vue's directive for binding event listeners to respond to user interactions.

  • Vue
<button @click="increment">+1</button>
Enter fullscreen mode Exit fullscreen mode
  • Compiled React
<button onClick={increment}>+1</button>
Enter fullscreen mode Exit fullscreen mode

As the example shows, Vue's @click directive is compiled into React's onClick attribute. VuReact adopts an event attribute compilation strategy, converting template directives into React's standard event attributes. This fully preserves Vue's event binding semantics — when the button is clicked, the increment function is called.

The key characteristics of this compilation approach are:

  1. Semantic consistency: Fully simulates Vue v-on behavior by implementing event listening functionality
  2. Naming conversion: Vue's @click is converted to React's onClick (camelCase naming)
  3. Function passing: Directly passes function references, preserving event handling logic
  4. React-native support: Uses React's standard event system with no additional adaptation required

With event modifiers: Advanced event handling

Vue's event system supports a rich set of modifiers for controlling event behavior. VuReact handles these modifiers through runtime helper functions.

  • Vue
<button @click.stop.prevent="submit">Submit</button>
Enter fullscreen mode Exit fullscreen mode
  • Compiled React
import { dir } from '@vureact/runtime-core';

<button onClick={dir.on('click.stop.prevent', submit)}>Submit</button>
Enter fullscreen mode Exit fullscreen mode

As the example shows, Vue events with modifiers are compiled using the dir.on() helper function. VuReact adopts a modifier runtime processing strategy, converting complex modifier combinations into runtime function calls. This fully preserves Vue's event modifier semantics.

Compilation strategy details:

// Vue: @click.stop.prevent="handler"
// React: onClick={dir.on('click.stop.prevent', handler)}

// Vue: @keyup.enter="search"
// React: onKeyUp={dir.on('keyup.enter', search)}

// Vue: @click.capture="captureHandler"
// React: onClickCapture={dir.on('click.capture', captureHandler)}
Enter fullscreen mode Exit fullscreen mode

How the runtime helper dir.on() works:

  1. Parse modifiers: Parses the event name and modifier string
  2. Create wrapper function: Creates an event handling wrapper function based on the modifiers
  3. Apply modifier logic: Implements the behavior corresponding to each modifier in the wrapper function
  4. Call original handler: Finally calls the developer-provided event handling function

Inline event handling and parameter passing

Vue supports writing inline event handling logic directly in templates, and VuReact handles this correctly as well.

  • Vue
<button @click="count++">Increment</button>
<button @click="sayHello('world')">Say Hello</button>
<button @click="handleEvent($event, 'custom')">With Event Object</button>
Enter fullscreen mode Exit fullscreen mode
  • Compiled React
<button onClick={() => count.value++}>Increment</button>
<button onClick={() => sayHello('world')}>Say Hello</button>
<button onClick={(event) => handleEvent(event, 'custom')}>With Event Object</button>
Enter fullscreen mode Exit fullscreen mode

Compilation strategy:

  1. Expression conversion: Converts Vue template expressions into JSX arrow functions
  2. Event object handling: Vue's $event is converted to React's event parameter
  3. Parameter passing: Preserves the argument order and values of function calls
  4. Reactive updates: Automatically handles .value access (for ref/computed variables, etc.)

defineEmits events and component communication

For component custom events, VuReact also has a corresponding compilation strategy.

  • Vue
<!-- Parent component -->
<Child @custom-event="handleCustom" />

<!-- Child component Child.vue -->
<template>
  <button @click="emits('custom-event', data)">Trigger Event</button>
</template>

<script setup>
const emits = defineEmits(['custom-event']);
</script>
Enter fullscreen mode Exit fullscreen mode
  • Compiled React
// Parent component usage
<Child onCustomEvent={handleCustom} />;

// Child component Child.jsx
function Child(props) {
  return <button onClick={() => props.onCustomEvent?.(data)}>Trigger Event</button>;
}
Enter fullscreen mode Exit fullscreen mode

Compilation rules:

  1. Event name conversion: kebab-case is converted to camelCase (custom-eventonCustomEvent)
  2. emit call conversion: emits() is converted to props callback invocations
  3. Optional chaining guard: Adds ?. optional chaining operator to prevent undefined errors
  4. Type safety: Preserves TypeScript type definition consistency

Compilation strategy summary

VuReact's event compilation strategy demonstrates a complete event system conversion capability:

  1. Basic event mapping: Precisely maps Vue event directives to React event attributes
  2. Modifier support: Fully supports Vue event modifiers through runtime helper functions
  3. Inline handling: Correctly handles inline event expressions in templates
  4. Custom events: Supports custom event communication between components
  5. Type safety: Preserves TypeScript type definition integrity

VuReact's compilation strategy ensures a smooth migration from Vue to React. Developers do not need to manually rewrite event handling logic. The compiled code preserves Vue's semantics and functionality while following React's event handling best practices, keeping the migrated application fully interactive.

Related Links

Top comments (0)