├── .babelrc ├── .gitignore ├── .npmignore ├── README.md ├── example ├── demo │ ├── .babelrc │ ├── .eslintrc.js │ ├── index.html │ ├── package-lock.json │ ├── package.json │ ├── rux.config.js │ ├── src │ │ ├── app.scss │ │ ├── app.tsx │ │ ├── assets │ │ │ ├── images │ │ │ │ └── alien.jpg │ │ │ └── styles │ │ │ │ └── common.scss │ │ ├── asyncRouter.js │ │ ├── index.js │ │ ├── mock.js │ │ ├── model │ │ │ └── index.ts │ │ └── page │ │ │ ├── goodsList │ │ │ ├── index.scss │ │ │ └── index.tsx │ │ │ ├── home │ │ │ ├── index.scss │ │ │ └── index.tsx │ │ │ ├── input │ │ │ └── index.tsx │ │ │ └── lifecycle │ │ │ ├── index.tsx │ │ │ └── style.scss │ └── tsconfig.json └── renderRoutesDemo.js ├── index.js ├── md ├── 111.gif ├── form.gif ├── lifecycle.gif └── scroll.gif ├── package-lock.json ├── package.json ├── rollup.config.js └── src ├── components ├── keepCache.js ├── keepliveRoute.js └── keepliveRouteSwitch.js ├── core ├── cacheContext.js └── keeper.js ├── hoc ├── extendsSelf.js └── lifecycle.js ├── index.js └── utils ├── const.js └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "presets": [ 4 | "@babel/preset-env", 5 | "@babel/preset-react" 6 | ], 7 | "plugins":[ 8 | "@babel/plugin-proposal-class-properties" 9 | ] 10 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | node_modules 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | docs 2 | node_modules 3 | src 4 | md 5 | .babelrc 6 | .gitignore 7 | .npmignore 8 | .prettierrc 9 | rollup.config.js 10 | yarn.lock 11 | example -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [](https://www.npmjs.org/package/react-keepalive-router) 4 | [](https://npmjs.org/package/react-keepalive-router) 5 |  6 | 7 | # react-keepalive-router 8 | 9 | 10 | ## 一 介绍 11 | 12 | 基于`react 16.8+` ,`react-router 4+` 开发的`react`缓存组件,可以用于缓存页面组件,类似`vue`的`keepalive`包裹`vue-router`的效果功能。 13 | 14 | 采用`react hooks`全新`api`,支持缓存路由,手动解除缓存,增加了**缓存的状态周期**,监听函数等。 15 | 16 | 后续版本会完善其他功能。 17 | 18 | 19 | 20 | ### demo 21 | 22 | #### 缓存组件 + 监听 23 | 24 | 25 | ## 二 快速上手 26 | 27 | 28 | ### 下载 29 | 30 | ```bash 31 | npm install react-keepalive-router --save 32 | # or 33 | yarn add react-keepalive-router 34 | ``` 35 | 36 | 37 | ### 使用 38 | 39 | ### 1 基本用法 40 | 41 | 42 | #### KeepaliveRouterSwitch 43 | 44 | 45 | `KeepaliveRouterSwitch`可以理解为常规的Switch,也可以理解为 `keepaliveScope`,我们**确保整个缓存作用域,只有一个 `KeepaliveRouterSwitch` 就可以了**。 46 | 47 | #### 常规用法 48 | 49 | ````jsx 50 | import { BrowserRouter as Router, Route, Redirect ,useHistory } from 'react-router-dom' 51 | import { KeepaliveRouterSwitch ,KeepaliveRoute ,addKeeperListener } from 'react-keepalive-router' 52 | 53 | const index = () => { 54 | useEffect(()=>{ 55 | /* 增加缓存监听器 */ 56 | addKeeperListener((history,cacheKey)=>{ 57 | if(history)console.log('当前激活状态缓存组件:'+ cacheKey ) 58 | }) 59 | },[]) 60 | return