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-show directive is 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 Vue 3's
v-showdirective usage.
Compilation Mapping
v-show conditional display
v-show is Vue's directive for conditionally showing or hiding an element. Unlike v-if, v-show does not remove the DOM element — it controls visibility through the CSS display property.
- Vue
<div v-show="active">Content</div>
- Compiled React
<div style={{ display: active ? '' : 'none' }}>Content</div>
As the example shows, Vue's v-show directive is compiled into a React inline style object. VuReact adopts a style conditional compilation strategy, converting template directives into JSX's style property. This fully preserves Vue's show/hide semantics — when active is truthy, the element displays normally (display: '' uses the default value); when active is falsy, the element is hidden (display: 'none').
The key advantages of this compilation approach are:
-
Semantic consistency: Fully simulates Vue
v-showbehavior by controlling visibility through CSS rather than removing the DOM - Performance optimization: Avoids frequent DOM creation/destruction, ideal for scenarios with frequent visibility toggling
-
CSS inheritance preserved: Uses
display: 'none'instead ofvisibility: 'hidden', ensuring the element is completely removed from the document flow - State preservation: Element states (such as form input values, scroll positions, etc.) are retained when hidden
VuReact's compilation strategy ensures a smooth migration from Vue to React. Developers do not need to manually rewrite show/hide logic — the compiled code preserves Vue's semantics while following React's best practices.
Top comments (0)