Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/StructureBuilder/react-keep-alive
A component that maintains component state and avoids repeated re-rendering.
https://github.com/StructureBuilder/react-keep-alive
Last synced: 3 months ago
JSON representation
A component that maintains component state and avoids repeated re-rendering.
- Host: GitHub
- URL: https://github.com/StructureBuilder/react-keep-alive
- Owner: StructureBuilder
- License: mit
- Created: 2019-03-03T12:19:19.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T17:13:58.000Z (about 2 years ago)
- Last Synced: 2024-10-29T03:08:32.685Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 977 KB
- Stars: 984
- Watchers: 13
- Forks: 105
- Open Issues: 75
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.MIT
Awesome Lists containing this project
- awesome-github-star - react-keep-alive - rendering. | StructureBuilder | 945 | (TypeScript)
- awesome-star-libs - StructureBuilder / react-keep-alive
README
React Keep Alive
[![npm](https://img.shields.io/npm/v/react-keep-alive.svg?style=for-the-badge)](https://www.npmjs.com/package/react-keep-alive) [![Travis (.org)](https://img.shields.io/travis/Sam618/react-keep-alive.svg?style=for-the-badge)](https://travis-ci.org/Sam618/react-keep-alive.svg?branch=master) [![LICENSE](https://img.shields.io/npm/l/react-keep-alive.svg?style=for-the-badge)](https://github.com/Sam618/react-keep-alive/blob/master/LICENSE.MIT) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/react-keep-alive.svg?style=for-the-badge)](https://www.npmjs.com/package/react-keep-alive) [![downloads](https://img.shields.io/npm/dm/react-keep-alive.svg?style=for-the-badge)](https://www.npmjs.com/package/react-keep-alive) [![typescript](https://img.shields.io/badge/language-typescript-blue.svg?style=for-the-badge)](https://www.typescriptlang.org/)
A component that maintains component state and avoids repeated re-rendering.
## β¨ Features
- 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.
- You will be able to use the latest React Hooks.
- Ability to manually control whether your components need to stay active.## π¦ Installation
React Keep Alive requires React 16.3 or later, but if you use React Hooks, you must be React 16.8 or higher.To use React Keep Alive with your React app:
```bash
npm install --save react-keep-alive
```## π¨ Usage
React Keep Alive provides ``, you must use `` to wrap the `` cache to take effect.```JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import {
Provider,
KeepAlive,
} from 'react-keep-alive';
import Test from './views/Test';ReactDOM.render(
,
document.getElementById('root'),
);
```## π‘ 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.
## π API Reference
### `Provider`
Since the cached components need to be stored, the `` must be rendered at the top of the application for the program to run properly.#### Props
`include`: Only components that match key will be cached. It can be a string, an array of strings, or a regular expression, eg:
```JavaScript
...
// or
...
// or
...
````exclude`: Any component that matches key will not be cached. It can be a string, an array of strings, or a regular expression.
`max`(`v2.5.2+`): If the maximum value is set, the value in the cache is deleted after it goes out.
#### Example
In the example below, the component is our root-level component. This means itβs at the very top of our component hierarchy.```javascript
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-keep-alive';
import App from './App';ReactDOM.render(
,
document.getElementById('root'),
);
```##### Usage with React Router and Mobx React
```JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
} from 'react-router-dom';
import {
Provider as MobxProvider,
} from 'mobx-react';
import {
Provider as KeepAliveProvider,
} from 'react-keep-alive';ReactDOM.render(
,
document.getElementById('root'),
);
```**Note**: You must put in and the React Router must be sure to be the **latest version**. Because React Keep Alive uses the **new Context**, you must ensure that the Router does the same. Please use the following command to install the latest version.
```bash
npm install react-router@next react-router-dom@next
```### `KeepAlive`
Children of `` will be cached, but we have to make sure that `` is inside ``.#### Props
`name`: Name must exist and need to ensure that all `` names under the current `` are unique(1.2.0 added, Replace key).`disabled`: When we don't need components for caching, we can disable it; the disabled configuration will only takes effect when the component's status changes from unactive to active.
`extra`(`v2.0.1+`): Additional data can be obtained through `bindLifecycle`.
**Note**: `` The innermost outer layer of the packaged component must have a real DOM tag.
#### Example
```JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
Switch,
Route,
Link,
} from 'react-router-dom';
import {
Provider,
KeepAlive,
} from 'react-keep-alive';class One extends React.Component {
render() {
return (
// a real DOM tag
This is One.
);
}
}class App extends React.Component {
render() {
return (
);
}
}ReactDOM.render(
,
document.getElementById('root'),
);
```##### Usage with `include` props of ``
```JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
Switch,
Route,
Link,
} from 'react-router-dom';
import {
Provider,
KeepAlive,
} from 'react-keep-alive';class One extends React.Component {
render() {
return (
This is One.
);
}
}class App extends React.Component {
render() {
return (
);
}
}ReactDOM.render(
,
document.getElementById('root'),
);
```**Note**: If you want to use the **lifecycle**, wrap the components in a `bindLifecycle` high-level component.
### `bindLifecycle`
Components that pass this high-level component wrap will have the **correct** lifecycle, and we have added two additional lifecycles, `componentDidActivate` and `componentWillUnactivate`.Lifecycle after adding:
![Lifecycle after adding](https://github.com/Sam618/react-keep-alive/raw/master/assets/lifecycle.png)`componentDidActivate` will be executed once after the initial mount or from the unactivated state to the active state. although we see `componentDidActivate` after `componentDidUpdate` in the `Updating` phase, this does not mean `componentDidActivate` Always triggered.
At the same time, only one of the lifecycles of `componentWillUnactivate` and `componentWillUnmount` is triggered. `componentWillUnactivate` is executed when caching is required; `componentWillUnmount` is executed without caching.
#### Example
```JavaScript
import React from 'react';
import {bindLifecycle} from 'react-keep-alive';@bindLifecycle
class Test extends React.Component {
render() {
return (
This is Test.
);
}
}
```### `useKeepAliveEffect`
`useKeepAliveEffect` will fire when the component enters and leaves; because the component will not be unmounted while it is still active, so if you use `useEffect`, that will not achieve the real purpose.**Note**: `useKeepAliveEffect` uses the latest React Hooks, so you must make sure React is the latest version.
#### Example
```JavaScript
import React from 'react';
import {useKeepAliveEffect} from 'react-keep-alive';function Test() {
useKeepAliveEffect(() => {
console.log("mounted");
return () => {
console.log("unmounted");
};
});
return (
This is Test.
);
}
```## π Issues
If you find a bug, please file an issue on [our issue tracker on GitHub](https://github.com/Sam618/react-keep-alive/issues).## π Changelog
Changes are tracked in the [CHANGELOG.md](https://github.com/Sam618/react-keep-alive/blob/master/CHANGELOG.md).## π License
React Keep Alive is available under the [MIT](https://github.com/Sam618/react-keep-alive/blob/master/LICENSE) License.