umi-plugin-keep-alive
Version:
<KeepAlive> for umi base on react-activation
82 lines (59 loc) • 1.76 kB
Markdown
[](https://npmjs.org/package/umi-plugin-keep-alive)
[](https://npmjs.org/package/umi-plugin-keep-alive)
`<KeepAlive>` for umijs base on [react-activation](https://github.com/CJY0208/react-activation)
English | [中文说明](./README.md)
https://codesandbox.io/s/umi-keep-alive-tabs-demo-knfxy
1. Install
```bash
npm install umi-plugin-keep-alive --save
yarn add umi-plugin-keep-alive
```
2. export `KeepAlive` from umi and wrap any component you want to be keeped
```javascript
import { useState } from 'react'
import { KeepAlive } from 'umi'
function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p>count: {count}</p>
<button onClick={() => setCount(count => count + 1)}>add</button>
</div>
)
}
export default function() {
const [show, setShow] = useState(true)
return (
<div>
<h1>Page index</h1>
{show && (
<KeepAlive>
<Counter />
</KeepAlive>
)}
<button onClick={() => setShow(show => !show)}>toggle</button>
</div>
)
}
```
TODO
All function of react-activation can be completely imported from umi
```javascript
import {
KeepAlive,
useActivate,
useUnactivate,
withActivation,
withAliveScope,
useAliveController
} from 'umi'
```
Visit [react-activation](https://github.com/CJY0208/react-activation) for full documentation
MIT