Back to KB
Difficulty
Intermediate
Read Time
7 min

What Does Vue 3 computed() Compile to in React with VuReact?

By Codcompass Team··7 min read

Transpiling Vue 3 Derived State: How VuReact Maps Computed Properties to React Hooks

Current Situation Analysis

React's approach to derived state relies heavily on useMemo, which requires developers to manually specify dependency arrays. While this offers explicit control, it introduces cognitive overhead and maintenance friction. Every time a dependency changes, the array must be updated; missing a dependency leads to stale closures, while over-specifying dependencies causes unnecessary recalculations.

Vue 3's computed() API eliminates this friction through automatic dependency tracking. The reactivity system proxies state access, recording dependencies at runtime so the derived value updates only when relevant sources change. For teams migrating Vue logic to React, or those seeking Vue's developer experience within a React codebase, bridging this gap is critical.

VuReact addresses this by compiling Vue 3 syntax into standard React components while preserving the automatic tracking semantics. The compilation maps Vue's computed() directly to a custom React hook, useComputed(), provided by the @vureact/runtime-core package. This allows React applications to benefit from lazy evaluation and automatic dependency tracking without manual array management.

WOW Moment: Key Findings

The compilation strategy fundamentally shifts how derived state is managed in React. By analyzing the output of the VuReact compiler, we can compare the operational characteristics of native React patterns against the compiled Vue approach.

ApproachDependency ManagementBoilerplate OverheadWritable SupportType Inference
React useMemoManual array requiredHigh (deps must be listed)No (read-only)Strong (native)
Vue 3 computedAutomatic trackingLow (implicit)Yes (get/set)Strong (generic)
VuReact useComputedAutomatic trackingLow (implicit)Yes (get/set)Strong (preserved)

Why this matters: The useComputed hook generated by VuReact provides the ergonomic benefits of Vue's reactivity system within React. Developers gain automatic dependency tracking and writable derived state, reducing the risk of stale data and simplifying complex state transformations. This is particularly valuable when porting business logic from Vue to React, as the core computation code remains structurally identical.

Core Solution

The compilation process transforms Vue's computed API into a React hook that emulates proxy-based tracking. The runtime maintains a dependency graph internally, ensuring the computation function executes only when accessed sources change.

1. Basic Derived State

The compiler converts a standard computed declaration into a useComputed hook call. The source state is typically wrapped in useReactive, which provides the reactive proxy behavior necessary for tracking.

Compiled TypeScript Example:

import { useReactive, useComputed } from '@vureact/runti

🎉 Mid-Year Sale — Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register — Start Free Trial

7-day free trial · Cancel anytime · 30-day money-back