React Activation screenshot

React Activation

Author Avatar Theme by Cjy0208
Updated: 21 May 2025
1938 Stars

Hack <KeepAlive /> for React

Overview:

The React Activation library provides a way to implement the <keep-alive /> functionality seen in Vue for React applications. It also mentions official support for <Offsreen /> in React 18.x, offering a more stable <KeepAlive /> function with babel pre-compilation. Additionally, it is compatible with React v16/v17/v18 and Preact v10+.

Features:

  • Implement <keep-alive /> functionality in React
  • Compatible with React v16/v17/v18 and Preact v10+
  • More stable <KeepAlive /> function with babel pre-compilation
  • Cache Control for managing cache manually
  • Supports lifecycle methods for both ClassComponent and FunctionComponent
  • Online Demo and more examples available
  • Closable tabs with react-router and umi

Installation:

To install the React Activation library, follow these steps:

  1. (Optional but recommended) Add react-activation/babel plugins in .babelrc file.

    "plugins": ["react-activation/babel"]
    

    This plugin adds a _nk attribute to each JSX element during compilation to help generate a unique identifier for the <KeepAlive> component.

  2. Wrap the components that need to keep states with <KeepAlive>, such as:

    <KeepAlive>
        <Counter />
    </KeepAlive>
    
  3. Place the <AliveScope> component at a location that will not be unmounted, usually at the application entrance.

Summary:

The React Activation library provides a way to implement Vue’s <keep-alive /> functionality in React applications. It also offers more stable <KeepAlive /> function with babel pre-compilation and compatibility with React v16/v17/v18 and Preact v10+. With features like cache control, lifecycle methods support, and examples for easy understanding, React Activation aims to enhance state management and performance optimization in React applications.