惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
月光博客
月光博客
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Vercel News
Vercel News
量子位
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
腾讯CDC
有赞技术团队
有赞技术团队

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How does VuReact compile Vue's KeepAlive component to React?
Ryan John · 2026-05-31 · via DEV Community

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 built-in <KeepAlive> component 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 <KeepAlive> component usage.

Compilation Mapping

KeepAlive: Component caching

<KeepAlive> is Vue's built-in component for caching component instances. It preserves component state during dynamic component switching, avoiding re-rendering and data loss.

Basic KeepAlive usage

  • Vue
<template>
  <KeepAlive>
    <component :is="currentView" />
  </KeepAlive>
</template>

  • Compiled React
import { KeepAlive } from '@vureact/runtime-core';

<KeepAlive>
  <Component is={currentView} />
</KeepAlive>

As the example shows, Vue's <KeepAlive> component is compiled into the KeepAlive adapter component provided by VuReact Runtime — think of it as "Vue's KeepAlive for React".

The key characteristics of this compilation approach are:

  1. Semantic consistency: Fully simulates Vue <KeepAlive> behavior by implementing component instance caching
  2. State preservation: Caches removed component instances, preventing state loss
  3. Performance optimization: Reduces unnecessary component re-rendering
  4. React adaptation: Implements Vue's caching semantics in the React environment

KeepAlive with key

To ensure caching works correctly, it is recommended to provide a stable key for dynamic components.

  • Vue
<template>
  <KeepAlive>
    <component :is="currentComponent" :key="componentKey" />
  </KeepAlive>
</template>

  • Compiled React
<KeepAlive>
  <Component is={currentComponent} key={componentKey} />
</KeepAlive>

The importance of key:

  1. Cache identifier: key is used to identify and match cached instances
  2. Stable switching: Ensures correct cache hits when switching components
  3. Performance optimization: Avoids unnecessary cache creation and destruction
  4. Best practice: Always provide a stable key for dynamic components

Include and exclude control

<KeepAlive> supports the include and exclude attributes for precise control over which components should be cached.

include: Include specific components

  • Vue
<template>
  <KeepAlive :include="['ComponentA', 'ComponentB']">
    <component :is="currentView" />
  </KeepAlive>
</template>

  • Compiled React
<KeepAlive include={['ComponentA', 'ComponentB']}>
  <Component is={currentView} />
</KeepAlive>

exclude: Exclude specific components

  • Vue
<template>
  <KeepAlive :exclude="['GuestPanel', /^Temp/]">
    <component :is="currentView" />
  </KeepAlive>
</template>

  • Compiled React
<KeepAlive exclude={['GuestPanel', /^Temp/]}>
  <Component is={currentView} />
</KeepAlive>

Matching rules:

  1. String matching: Exact match on component name
  2. Regular expression: Matches component names matching the pattern
  3. Array combination: Supports arrays of strings and regular expressions
  4. Key matching: Attempts to match both component name and cache key

Maximum cache instances

The max attribute limits the maximum number of cached instances, preventing excessive memory usage.

  • Vue
<template>
  <KeepAlive :max="3">
    <component :is="currentTab" />
  </KeepAlive>
</template>

  • Compiled React
<KeepAlive max={3}>
  <Component is={currentTab} />
</KeepAlive>

Cache eviction strategy:

  1. LRU algorithm: Evicts the least recently accessed cached instance
  2. Memory management: Automatically clears cache exceeding the limit
  3. Performance balance: Strikes a balance between memory usage and performance
  4. Intelligent management: Manages cache intelligently based on access frequency

Cache lifecycle

Components cached by <KeepAlive> have special lifecycle hooks that can be observed.

Activated and deactivated lifecycle

  • Vue
<script setup>
import { onActivated, onDeactivated } from 'vue';

onActivated(() => {
  console.log('Component activated');
});

onDeactivated(() => {
  console.log('Component deactivated');
});
</script>

  • Compiled React
import { useActived, useDeactivated } from '@vureact/runtime-core';

function MyComponent() {
  useActived(() => {
    console.log('Component activated');
  });

  useDeactivated(() => {
    console.log('Component deactivated');
  });

  return <div>Component content</div>;
}

Lifecycle events:

  1. useActived: Triggered when the component is restored from cache and displayed
  2. useDeactivated: Triggered when the component is cached
  3. Initial render: Activated is also triggered on the component's initial render
  4. Final unmount: Deactivated is triggered when the component is finally destroyed

Compilation strategy summary

VuReact's KeepAlive compilation strategy demonstrates a complete component caching conversion capability:

  1. Direct component mapping: Maps Vue <KeepAlive> directly to VuReact's <KeepAlive>
  2. Full attribute support: Supports all attributes including include, exclude, max, etc.
  3. Lifecycle adaptation: Converts Vue lifecycle hooks into React Hooks
  4. Cached semantics preserved: Fully preserves Vue's caching behavior and semantics

How KeepAlive works:

  1. Instance caching: Preserves the instance in memory when a component is switched out
  2. State preservation: Keeps all of the component's state and data
  3. DOM retention: Retains the component's DOM structure
  4. Smart restoration: Quickly restores the previous instance when switching back

Performance optimization strategy:

  1. On-demand caching: Only caches components that truly need it
  2. Memory management: Intelligently manages cache memory usage
  3. Fast restoration: Optimizes cache restoration performance
  4. Garbage collection: Timely cleanup of cache that is no longer needed

Important notes:

  1. Single child node: <KeepAlive> can only have one direct child node
  2. Component type: Can only cache component elements, not regular elements
  3. Key requirement: Without a stable key, it degrades to non-cached rendering

VuReact's compilation strategy ensures a smooth migration from Vue to React. Developers do not need to manually implement component caching logic. The compiled code preserves Vue's caching semantics and performance advantages while following React's component design patterns, keeping the migrated application fully capable of component caching.

Related Links