Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Sunny-117/rka

A component that maintains component state and avoids repeated re-rendering.
https://github.com/Sunny-117/rka

cache react-keepalive reactjs

Last synced: about 2 months ago
JSON representation

A component that maintains component state and avoids repeated re-rendering.

Awesome Lists containing this project

README

        

# rkajs [![npm](https://img.shields.io/npm/v/@sunny-117/cherry.svg)](https://npmjs.com/package/rkajs)





## ✨ Features
- Lightweight
- You will be able to use the latest React Hooks.
- Not based on React Router, so you can use it wherever you need to cache it.
- You can easily use `` to wrap your components to keep them alive.
- Because it is not controlled by `display: none | block`, you can use animation.
- Ability to manually control whether your components need to stay active.

## 📦 Installation

To use rka with your React app:

```bash
npm install rkajs
yarn add rkajs
pnpm i rkajs
bun i rkajs
```

## Usage

![views](./assets/views.gif)

```tsx
import { KeepAlive, KeepAliveTransfer } from "rkajs";
import { BrowserRouter, Link, Routes, Route } from "react-router-dom";
import Home from "./pages/Home";
import Form from "./pages/Form";

const AliveHomeView = KeepAliveTransfer(Home, "home");
const AliveFormView = KeepAliveTransfer(Form, "form");

export default function App() {
return (





  • Home


  • Form




}>
}
>





);
}
```

## 💡 Why do you need this component?

If you've used [Vue](https://vuejs.org/), you know that it has a very good component ([keep-alive](https://vuejs.org/v2/guide/components-dynamic-async.html)) that keeps the state of the component to avoid repeated re-rendering.

Sometimes, we want the list page to cache the page state after the list page enters the detail page. When the detail page returns to the list page, the list page is still the same as before the switch.

Oh, this is actually quite difficult to achieve, because the components in React cannot be reused once they are uninstalled. Two solutions are proposed in [issue #12039](https://github.com/facebook/react/issues/12039). By using the style switch component display (`display: none | block;`), this can cause problems, such as when you switch components, you can't use animations; or use data flow management tools like Mobx and Redux, but this is too much trouble.

In the end, I implemented this effect through the [React.createPortal API](https://reactjs.org/docs/portals.html). `react-keep-alive` has two main components `` and ``. The `` is responsible for saving the component's cache and rendering the cached component outside of the application via the React.createPortal API before processing. The cached components must be placed in ``, and `` will mount the components that are cached outside the application to the location that really needs to be displayed.

## 🐛 Issues

If you find a bug, please file an issue on [our issue tracker on GitHub](https://github.com/Sunny-117/rka/issues).

## 📄 License

rks is available under the [MIT](./LICENSE) License.

## Inspired

Inspired by [react-keep-alive](https://github.com/StructureBuilder/react-keep-alive/)