VuReact is a compiler for migrating from Vue to React — and for writing React with Vue syntax. In this article, we dive straight into the core: how Vue's common is and :is attributes are compiled into React code by VuReact.
Before We Start
To keep the examples easy to read, this article follows two simple conventions:
- All Vue and React snippets focus on core logic only, with full component wrappers and unrelated configuration omitted.
- The discussion assumes you are already familiar with the usage and use cases of Vue 3's
isattribute.
Compilation Mapping
Dynamic components: :is attribute
The :is attribute is used for dynamically rendering components. It allows you to decide which component to render based on data at runtime.
Basic dynamic components
- Vue
<component :is="currentComponent" />
- Compiled React
import { Component } from '@vureact/runtime-core';
<Component is={currentComponent} />
As the example shows, Vue's :is attribute is compiled into the is prop of the Component adapter component — think of it as "Vue's dynamic component for React".
The key characteristics of this compilation approach are:
- Semantic consistency: Fully simulates Vue's dynamic component behavior by implementing component switching
-
Component mapping: Converts Vue's
<component>element into VuReact Runtime's<Component>component -
Attribute passing: Preserves the dynamic binding capability of the
isattribute
Dynamic components with props
Dynamic components often need props passed to them, and VuReact handles this correctly.
- Vue
<component
:is="currentView"
:title="pageTitle"
:data="pageData"
@custom-event="handleCustom"
/>
- Compiled React
<Component
is={currentView}
title={pageTitle}
data={pageData}
onCustomEvent={handleCustom}
/>
Props passing rules:
- Attribute mapping: Converts Vue attributes to React props
- Event conversion: Converts Vue events to React event props
- Type preservation: Keeps the type definition integrity of props
- Default value handling: Correctly handles component default props
Resolving DOM template limitations: is attribute
The is attribute is used to resolve in-DOM template parsing limitations, primarily for correctly rendering components inside elements like <table>, <ul>, <ol>, <select>, etc.
Using is in tables
- Vue
<table>
<tr is="vue:user-row"></tr>
</table>
- Compiled React
<table>
<UserRow />
</table>
Compilation strategy:
-
Component replacement: Replaces
is="vue:user-row"with<UserRow /> -
Vue prefix handling: Automatically removes the
vue:prefix - HTML structure preservation: Maintains correct HTML element nesting structure
Using is in lists
- Vue
<ul>
<li is="vue:todo-item"></li>
</ul>
- Compiled React
<ul>
<TodoItem />
</ul>
Dynamic components with v-bind
Dynamic components are often used together with v-bind for more flexible component configuration.
- Vue
<component
:is="componentType"
v-bind="componentProps"
/>
- Compiled React
import { dir } from '@vureact/runtime-core';
<Component
is={componentType}
{...dir.keyless(componentProps)}
/>
Object spread handling:
-
Attribute merging: Correctly handles merging of
v-bindobjects with explicit attributes - Conflict resolution: Handles attribute name conflicts
-
Special attribute conversion: Automatically converts special attributes like
class,style, etc.
Compilation strategy summary
VuReact's is/:is compilation strategy demonstrates a complete dynamic component conversion capability:
-
Dynamic component rendering: Converts
<component :is>to<Component is> -
DOM limitation resolution: Directly replaces
is="vue:component-name"with the component - Props passing: Correctly handles props passing for dynamic components
-
Component caching: Supports
<KeepAlive>component caching -
Animation support: Supports
<Transition>component animations
Difference between is and :is:
| Feature |
is attribute |
:is attribute |
|---|---|---|
| Purpose | Resolves in-DOM template limitations | Dynamically switches components |
| Syntax | is="vue:component-name" |
:is="componentName" |
| Element | Used inside specific HTML elements | Used with <component> element |
| Compilation result | Directly replaced with component | Uses <Component is={...}>
|
VuReact's compilation strategy ensures a smooth migration from Vue to React. Developers do not need to manually rewrite dynamic component logic. The compiled code preserves Vue's semantics and flexibility while following React's component rendering patterns, keeping the migrated application fully capable of dynamic component switching.
Top comments (0)