Commit 0d58f185 authored by Shen Chang's avatar Shen Chang

docs(README): Refactoring documents

parent ad818ddd
# Online Editor
- Basic
- With React Router
- Control cache
- Using animation
# React Keep Alive
Package will allow components to maintain their status, to avoid repeated re-rendering.
<p align="center">
<a href="https://github.com/Sam618/react-keep-alive">
<img width="120" src="./assets/logo.svg">
</a>
</p>
<h1 align="center">React Keep Alive</h1>
<div align="center">
## TODO
- test
[![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) [![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/)
<p>A component that maintains component state and avoids repeated re-rendering.</p>
<div style="width: 100px; text-align: left;">
<div>English | <a href="./README.zh-CN.md">中文</a></div>
<div><a href="./ONLINE_EDITOR.md">Online Editor</a></div>
</div>
</div>
## Installation
React Keep Alive requires React 16.3 or later.
......@@ -26,75 +37,29 @@ import {
Provider,
KeepAlive,
} from 'react-keep-alive';
class Test extends React.Component {
state = {
count: 0,
};
handleClick = () => {
this.setState(({count}) => ({
count: count + 1,
}));
}
render() {
const {count} = this.state;
return (
<div>
<h1>This is {`<Test>`}</h1>
<p>Please feel free to click the button, then hide this component, and the status will be retained again.</p>
<button onClick={this.handleClick}>Click me(count: {count})</button>
</div>
);
}
}
class App extends React.Component {
state = {
hidden: false,
};
handleClick = () => {
this.setState(({hidden}) => ({
hidden: !hidden,
}));
}
render() {
const {hidden} = this.state;
return (
<div>
<div>
Clicking the button will hide the component, but the status will be preserved.
</div>
<button onClick={this.handleClick}>Click me(hidden: {hidden.toString()})</button>
<div>
{
!hidden
? (
// Must have a key, and it is unique
<KeepAlive key="Test">
<Test />
</KeepAlive>
)
: null
}
</div>
</div>
);
}
}
import Test from './views/Test';
ReactDOM.render(
<Provider>
<App />
<KeepAlive key="Test">
<Test />
</KeepAlive>
</Provider>,
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 `<Provider>` and `<KeepAlive>`. The `<Provider>` 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 `<KeepAlive>`, and `<KeepAlive>` will mount the components that are cached outside the application to the location that really needs to be displayed.
## API Reference
### `Provider`
......@@ -198,49 +163,16 @@ class One extends React.Component {
}
}
class Two extends React.Component {
render() {
return (
<div>This is Two.</div>
);
}
}
class App extends React.Component {
handleActivate = () => {
console.log('One activated');
}
handleUnactivate = () => {
console.log('One unactivated');
}
render() {
return (
<div>
<ul>
<li>
<Link to="/one">one</Link>
</li>
<li>
<Link to="/two">two</Link>
</li>
</ul>
<Switch>
<Route path="/one">
<KeepAlive
key="One"
onActivate={this.handleActivate}
onUnactivate={this.handleUnactivate}
>
<KeepAlive key="One">
<One />
</KeepAlive>
</Route>
<Route path="/two">
<KeepAlive key="Two">
<Two />
</KeepAlive>
</Route>
</Switch>
</div>
);
......@@ -270,19 +202,9 @@ import {
import {
Provider,
KeepAlive,
bindLifecycle,
} from 'react-keep-alive';
@bindLifecycle
class One extends React.Component {
componentDidMount() {
console.log('One componentDidMount');
}
componentWillUnmount() {
console.log('One componentWillUnmount');
}
render() {
return (
<div>This is One.</div>
......@@ -290,49 +212,16 @@ class One extends React.Component {
}
}
class Two extends React.Component {
render() {
return (
<div>This is Two.</div>
);
}
}
class App extends React.Component {
handleActivate = () => {
console.log('One activated');
}
handleUnactivate = () => {
console.log('One unactivated');
}
render() {
return (
<div>
<ul>
<li>
<Link to="/one">one</Link>
</li>
<li>
<Link to="/two">two</Link>
</li>
</ul>
<Switch>
<Route path="/one">
<KeepAlive
key="One"
onActivate={this.handleActivate}
onUnactivate={this.handleUnactivate}
>
<KeepAlive key="One">
<One />
</KeepAlive>
</Route>
<Route path="/two">
<KeepAlive key="Two">
<Two />
</KeepAlive>
</Route>
</Switch>
</div>
);
......
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1552810005619" class="icon" style="" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7010" data-spm-anchor-id="a313x.7781069.0.i17" xmlns:xlink="http://www.w3.org/1999/xlink" width="128" height="128"><defs><style type="text/css"></style></defs><path d="M1024.213333 510.186667c0-67.733333-84.8-131.946667-214.933333-171.733334C839.253333 205.866667 825.92 100.373333 767.146667 66.56c-13.546667-7.893333-29.44-11.626667-46.72-11.626667v46.506667c9.6 0 17.28 1.92 23.786666 5.44 28.373333 16.213333 40.64 78.186667 31.04 157.76-2.24 19.626667-6.08 40.213333-10.666666 61.333333-40.853333-10.026667-85.44-17.706667-132.373334-22.72-28.16-38.613333-57.28-73.6-86.72-104.213333 67.946667-63.146667 131.733333-97.813333 175.146667-97.813333V54.72c-57.28 0-132.373333 40.853333-208.213333 111.786667-75.84-70.506667-150.933333-110.933333-208.213334-110.933334v46.506667c43.2 0 107.2 34.346667 175.146667 97.173333-29.226667 30.613333-58.346667 65.493333-86.08 104-47.146667 5.013333-91.733333 12.693333-132.586667 22.933334-4.8-20.8-8.32-41.066667-10.88-60.48-9.813333-79.68 2.24-141.546667 30.4-157.973334 6.293333-3.733333 14.4-5.44 24-5.44V55.786667c-17.493333 0-33.386667 3.733333-47.146666 11.626666-58.56 33.813333-71.68 139.093333-41.493334 271.253334C85.866667 378.666667 1.493333 442.666667 1.493333 510.186667c0 67.733333 84.8 131.946667 214.933334 171.733333-29.973333 132.586667-16.64 238.08 42.133333 271.786667 13.546667 7.893333 29.44 11.626667 46.933333 11.626666 57.28 0 132.373333-40.853333 208.213334-111.786666 75.84 70.506667 150.933333 110.933333 208.213333 110.933333 17.493333 0 33.386667-3.733333 47.146667-11.626667 58.56-33.813333 71.68-139.093333 41.493333-271.253333 129.173333-39.68 213.653333-103.893333 213.653333-171.413333zM752.746667 371.2c-7.68 26.88-17.28 54.613333-28.16 82.346667-8.533333-16.64-17.493333-33.386667-27.306667-50.026667-9.6-16.64-19.84-32.96-29.973333-48.746667 29.653333 4.266667 58.133333 9.706667 85.44 16.426667z m-95.466667 221.973333c-16.213333 28.16-32.96 54.826667-50.24 79.68-31.04 2.666667-62.506667 4.16-94.186667 4.16-31.466667 0-62.933333-1.493333-93.76-3.946666-17.28-24.853333-34.24-51.306667-50.453333-79.253334-15.893333-27.306667-30.186667-55.04-43.413333-82.986666 12.906667-27.946667 27.52-55.893333 43.2-83.2 16.213333-28.16 32.96-54.826667 50.24-79.68 31.04-2.666667 62.506667-4.16 94.186666-4.16 31.466667 0 62.933333 1.493333 93.76 3.946666 17.28 24.853333 34.24 51.306667 50.453334 79.253334 15.893333 27.306667 30.186667 55.04 43.413333 82.986666-13.12 27.946667-27.52 55.893333-43.2 83.2z m67.413333-27.093333c11.306667 27.946667 20.8 55.893333 28.8 82.986667-27.306667 6.72-56.106667 12.266667-85.866666 16.64 10.24-16 20.48-32.533333 29.973333-49.386667 9.493333-16.64 18.453333-33.6 27.093333-50.24zM513.28 788.48c-19.413333-20.053333-38.826667-42.346667-57.92-66.666667 18.773333 0.853333 37.973333 1.493333 57.28 1.493334 19.626667 0 38.933333-0.426667 57.92-1.493334-18.773333 24.32-38.08 46.72-57.28 66.666667zM358.186667 665.706667c-29.653333-4.373333-58.133333-9.813333-85.44-16.426667 7.68-26.88 17.28-54.613333 28.16-82.346667 8.533333 16.64 17.493333 33.386667 27.306666 50.026667 9.706667 16.64 19.733333 32.96 29.973334 48.746667z m154.026666-433.813334c19.413333 20.053333 38.826667 42.346667 57.92 66.666667-18.773333-0.853333-37.973333-1.493333-57.28-1.493333-19.626667 0-38.933333 0.426667-57.92 1.493333 18.773333-24.32 38.08-46.613333 57.28-66.666667zM357.973333 354.666667c-10.24 16-20.48 32.533333-29.973333 49.386666-9.6 16.64-18.56 33.386667-27.093333 50.026667-11.306667-27.946667-20.8-55.893333-28.8-82.986667 27.306667-6.4 56-12.053333 85.866666-16.426666zM169.28 615.68C95.466667 584.213333 47.786667 542.933333 47.786667 510.186667s47.786667-74.24 121.493333-105.493334c17.92-7.68 37.546667-14.613333 57.706667-21.013333 11.84 40.853333 27.52 83.413333 46.933333 126.933333-19.2 43.413333-34.56 85.653333-46.293333 126.293334-20.586667-6.4-40.213333-13.546667-58.346667-21.226667z m112.213333 297.92c-28.373333-16.213333-40.64-78.186667-31.04-157.76 2.24-19.626667 6.08-40.213333 10.666667-61.333333 40.853333 10.026667 85.44 17.706667 132.373333 22.72 28.16 38.613333 57.28 73.6 86.72 104.213333-67.946667 63.146667-131.733333 97.813333-175.146666 97.813333-9.386667-0.213333-17.386667-2.133333-23.573334-5.653333z m494.4-158.826667c9.813333 79.68-2.24 141.546667-30.4 157.973334-6.293333 3.733333-14.4 5.44-24 5.44-43.2 0-107.2-34.346667-175.146666-97.173334 29.226667-30.613333 58.346667-65.493333 86.08-104 47.146667-5.013333 91.733333-12.693333 132.586666-22.933333 4.906667 21.013333 8.64 41.28 10.88 60.693333z m80.32-139.093333c-17.92 7.68-37.546667 14.613333-57.706666 21.013333-11.84-40.853333-27.52-83.413333-46.933334-126.933333 19.2-43.413333 34.56-85.653333 46.293334-126.293333 20.586667 6.506667 40.213333 13.546667 58.56 21.226666 73.813333 31.466667 121.493333 72.746667 121.493333 105.493334-0.213333 32.746667-47.893333 74.24-121.706667 105.493333zM304 55.573333z" fill="#d81e06" p-id="7011" data-spm-anchor-id="a313x.7781069.0.i15" class=""></path><path d="M512.64 510.186667m-95.253333 0a95.253333 95.253333 0 1 0 190.506666 0 95.253333 95.253333 0 1 0-190.506666 0Z" fill="#1296db" p-id="7012" data-spm-anchor-id="a313x.7781069.0.i14" class="selected"></path><path d="M720.32 54.933333z" fill="#05A5D1" p-id="7013"></path></svg>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment