Dynamic Component Semantic Comparison
How do Vue's common is and :is attributes transform into React code after VuReact compilation?
Prerequisites
To avoid redundancy in example code and ensure clear understanding, let's establish two conventions:
- Vue/React code examples in this document are simplified core logic, omitting full component wrappers and unrelated configurations.
- Readers are assumed to be familiar with the usage and applicable scenarios of the
isattribute in Vue 3.
:is → React <Component> Adapter Component
The :is attribute is used for dynamic component rendering, allowing components to be rendered based on data.
- Vue code:
<component :is="currentComponent" />- VuReact compiled React code:
import { Component } from '@vureact/runtime-core';
<Component is={currentComponent} />;As shown in the example: Vue's :is attribute is compiled into the is attribute of the Component adapter component, which can be understood as "Vue dynamic components in React style."
Key characteristics of this compilation approach:
- Semantic consistency: Fully mimics Vue dynamic component behavior for component switching
- Component mapping: Converts Vue's
<component>element to VuReact Runtime's<Component>component - Property passing: Maintains dynamic binding capability of the
isattribute
Dynamic Components with props
Dynamic components often need to pass props, which VuReact also handles correctly.
- Vue code:
<component :is="currentView" :title="pageTitle" :data="pageData" @custom-event="handleCustom" />- VuReact compiled React code:
<Component is={currentView} title={pageTitle} data={pageData} onCustomEvent={handleCustom} />Props passing rules:
- Property mapping: Converts Vue attributes to React props
- Event conversion: Converts Vue events to React event properties
- Type preservation: Maintains completeness of prop type definitions
- Default value handling: Correctly handles component default props
Plain String is
- Vue code:
<div is="my-tag"></div>- VuReact compiled React code:
<div is="my-tag" />As shown in the example: Vue's is attribute remains unchanged after compilation because React also supports the is attribute.
Key characteristics of this handling approach:
- Attribute preservation: Maintains the original value of the
isattribute - DOM compatibility: Ensures correct rendering in React
- Semantic consistency: Maintains the same semantics as Vue
With 'vue:' Prefix → React Component Direct Replacement
- Vue code:
<div is="vue:user-info"></div>- VuReact compiled React code:
<UserInfo />Compilation strategy:
- Component replacement: Replaces
is="vue:user-info"with<UserInfo /> - Vue prefix handling: Automatically removes the
vue:prefix
Dynamic Components Combined with v-bind
Dynamic components are often used with v-bind for more flexible component configuration.
- Vue code:
<component :is="componentType" v-bind="componentProps" />- VuReact compiled React code:
import { dir } from '@vureact/runtime-core';
<Component is={componentType} {...dir.keyless(componentProps)} />;Object spread handling:
- Property merging: Correctly handles merging of
v-bindobjects with explicit properties - Conflict resolution: Handles property name conflicts
- Special property conversion: Automatically converts special properties like
class,style, etc.
Summary
VuReact's is/:is compilation strategy demonstrates complete dynamic component transformation capability:
- Dynamic component rendering: Converts
<component :is>to<Component is> - DOM limitation resolution: Directly replaces
is="vue:component-name"with components - Props passing: Correctly handles props passing for dynamic components
- Component caching: Supports
<KeepAlive>component caching - Animation support: Supports
<Transition>component animations
Differences between is and :is:
| Feature | is attribute | :is attribute |
|---|---|---|
| Purpose | Resolves DOM template restrictions | 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 smooth migration from Vue to React, eliminating the need for developers to manually rewrite dynamic component logic. The compiled code maintains both Vue's semantics and flexibility while adhering to React's component rendering patterns, preserving complete dynamic component capabilities in migrated applications.
