├── .gitignore ├── md ├── 111.gif ├── form.gif ├── scroll.gif └── lifecycle.gif ├── example ├── demo │ ├── src │ │ ├── page │ │ │ ├── lifecycle │ │ │ │ ├── style.scss │ │ │ │ └── index.tsx │ │ │ ├── home │ │ │ │ ├── index.scss │ │ │ │ └── index.tsx │ │ │ ├── input │ │ │ │ └── index.tsx │ │ │ └── goodsList │ │ │ │ ├── index.tsx │ │ │ │ └── index.scss │ │ ├── assets │ │ │ ├── images │ │ │ │ └── alien.jpg │ │ │ └── styles │ │ │ │ └── common.scss │ │ ├── index.js │ │ ├── app.scss │ │ ├── model │ │ │ └── index.ts │ │ ├── asyncRouter.js │ │ ├── app.tsx │ │ └── mock.js │ ├── rux.config.js │ ├── tsconfig.json │ ├── .babelrc │ ├── index.html │ ├── package.json │ └── .eslintrc.js └── renderRoutesDemo.js ├── src ├── core │ ├── cacheContext.js │ └── keeper.js ├── hoc │ ├── extendsSelf.js │ └── lifecycle.js ├── index.js ├── utils │ ├── const.js │ └── index.js └── components │ ├── keepliveRouteSwitch.js │ ├── keepCache.js │ └── keepliveRoute.js ├── .npmignore ├── .babelrc ├── index.js ├── rollup.config.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | node_modules 3 | -------------------------------------------------------------------------------- /md/111.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodLuckAlien/react-keepalive-router/HEAD/md/111.gif -------------------------------------------------------------------------------- /example/demo/src/page/lifecycle/style.scss: -------------------------------------------------------------------------------- 1 | .box{ 2 | width: 500px; 3 | padding: 30px; 4 | } -------------------------------------------------------------------------------- /md/form.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodLuckAlien/react-keepalive-router/HEAD/md/form.gif -------------------------------------------------------------------------------- /md/scroll.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodLuckAlien/react-keepalive-router/HEAD/md/scroll.gif -------------------------------------------------------------------------------- /md/lifecycle.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodLuckAlien/react-keepalive-router/HEAD/md/lifecycle.gif -------------------------------------------------------------------------------- /example/demo/rux.config.js: -------------------------------------------------------------------------------- 1 | module.exports ={ 2 | dev:{ 3 | }, 4 | pro:{ 5 | }, 6 | base:{ 7 | } 8 | } -------------------------------------------------------------------------------- /example/demo/src/assets/images/alien.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoodLuckAlien/react-keepalive-router/HEAD/example/demo/src/assets/images/alien.jpg -------------------------------------------------------------------------------- /src/core/cacheContext.js: -------------------------------------------------------------------------------- 1 | import {createContext} from 'react' 2 | 3 | const cacheRouterContext = createContext() 4 | 5 | export default cacheRouterContext -------------------------------------------------------------------------------- /.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 -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "presets": [ 4 | "@babel/preset-env", 5 | "@babel/preset-react" 6 | ], 7 | "plugins":[ 8 | "@babel/plugin-proposal-class-properties" 9 | ] 10 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | if (process.env.NODE_ENV === 'production') { 4 | module.exports = require('./lib/index.min.js'); 5 | } else { 6 | module.exports = require('./lib/index.js'); 7 | } -------------------------------------------------------------------------------- /example/demo/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import App from './app' 4 | import rux from 'ruxjs' 5 | import './assets/styles/common.scss' 6 | 7 | 8 | ReactDOM.render( 9 | rux.create({}, () => < App / > ), 10 | document.getElementById('app') 11 | ) 12 | 13 | -------------------------------------------------------------------------------- /src/hoc/extendsSelf.js: -------------------------------------------------------------------------------- 1 | import { isFuntion } from '../utils/index' 2 | 3 | export default function ExtendsSelfHoc(Componet,getCurrent){ 4 | return class Hoc extends Componet{ 5 | constructor(props){ 6 | super(props) 7 | isFuntion(getCurrent) && getCurrent(this) 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /example/demo/src/page/home/index.scss: -------------------------------------------------------------------------------- 1 | .box{ 2 | margin-top: 50px; 3 | } 4 | 5 | .item{ 6 | height: 50px; 7 | line-height: 50px; 8 | border-radius: 20px; 9 | width: 200px; 10 | font-size: 15px; 11 | background-color: orange; 12 | color: #fff; 13 | font-weight: bold; 14 | text-align: center; 15 | margin-bottom: 20px; 16 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 2 | import KeepaliveRouterSwitch from './components/keepliveRouteSwitch' 3 | import KeepaliveRoute from './components/keepliveRoute' 4 | import {GetCacheContext, useCacheDispatch} from './components/keepCache' 5 | 6 | import cacheRouterContext from './core/cacheContext' 7 | import {addKeeperListener} from './core/keeper' 8 | 9 | import keepaliveLifeCycle from './hoc/lifecycle' 10 | 11 | export {KeepaliveRoute, KeepaliveRouterSwitch, cacheRouterContext, GetCacheContext, useCacheDispatch, addKeeperListener,keepaliveLifeCycle} 12 | -------------------------------------------------------------------------------- /example/demo/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strictNullChecks": true, 4 | "moduleResolution": "node", 5 | "allowSyntheticDefaultImports": true, 6 | "experimentalDecorators": true, 7 | "jsx": "react", 8 | "noUnusedParameters": true, 9 | "noUnusedLocals": true, 10 | "target": "es6", 11 | "module": "esnext", 12 | "lib": [ 13 | "dom", 14 | "es7" 15 | ] 16 | }, 17 | "exclude": [ 18 | "node_modules", 19 | "lib", 20 | "es" 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /src/utils/const.js: -------------------------------------------------------------------------------- 1 | export const KEEPLIVE_ROUTE_SWITCH = 'KEEPLIVE_ROUTE_SWITCH' 2 | 3 | export const KEEPLIVE_ROUTE_COMPONENT = 'KEEPLIVE_ROUTE_COMPONENT' 4 | 5 | export const ACITON_CREATED = 'created' /* 缓存创建 */ 6 | export const ACTION_ACTIVE = 'active' /* 缓存激活 */ 7 | export const ACTION_ACTIVED = 'actived' /* 激活完成 */ 8 | export const ACITON_UNACTIVE = 'unActive' /* 缓存休眠 */ 9 | export const ACTION_UNACTIVED = 'unActived' /* 休眠完成 */ 10 | export const ACTION_RESERT = 'reset' /* 设置摧毁状态 */ 11 | export const ACTION_DESTORYED = 'destoryed' /* 摧毁缓存 */ 12 | export const ACTION_CLEAR = 'clear' /* 清除缓存 */ -------------------------------------------------------------------------------- /example/demo/src/assets/styles/common.scss: -------------------------------------------------------------------------------- 1 | #app{ 2 | height: 100%; 3 | width: 100%; 4 | section{ 5 | height: 100%; 6 | width: 100%; 7 | } 8 | } 9 | #components-layout-demo-custom-trigger .trigger { 10 | font-size: 18px; 11 | line-height: 64px; 12 | padding: 0 24px; 13 | cursor: pointer; 14 | transition: color 0.3s; 15 | } 16 | 17 | #components-layout-demo-custom-trigger .trigger:hover { 18 | color: #1890ff; 19 | } 20 | 21 | #components-layout-demo-custom-trigger .logo { 22 | height: 32px; 23 | background: rgba(255, 255, 255, 0.2); 24 | margin: 16px; 25 | } 26 | 27 | .site-layout .site-layout-background { 28 | background: #fff; 29 | } -------------------------------------------------------------------------------- /example/demo/src/app.scss: -------------------------------------------------------------------------------- 1 | .page{ 2 | position: fixed; 3 | left:0; 4 | right: 0; 5 | top:0; 6 | bottom: 0; 7 | background-color: #ffff; 8 | 9 | } 10 | .content{ 11 | position: absolute; 12 | left:50%; 13 | top: 50%; 14 | transform: translate(-50%,-50%); 15 | } 16 | .title{ 17 | text-align: center; 18 | font-size: 10px; 19 | color: #ccc; 20 | } 21 | .btns{ 22 | padding-left: 30px; 23 | button{ 24 | margin-right: 30px; 25 | } 26 | } 27 | .routerLink{ 28 | margin-left: 15px; 29 | color: #0366d6; 30 | font-size: 14px; 31 | } 32 | .theStyle{ 33 | height: 50px; 34 | background-color: #fff; 35 | position: fixed; 36 | left:0; 37 | right:0; 38 | top:0; 39 | z-index: 10000; 40 | } -------------------------------------------------------------------------------- /example/demo/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "modules":"commonjs", 7 | "targets": { 8 | "chrome": "67" 9 | }, 10 | "useBuiltIns": "usage", 11 | "corejs": 2 12 | } 13 | ], 14 | "@babel/preset-react" 15 | ], 16 | "plugins": [ 17 | "@babel/plugin-proposal-class-properties", 18 | [ 19 | "@babel/plugin-transform-runtime", 20 | { 21 | "absoluteRuntime": false, 22 | "helpers": true, 23 | "regenerator": true, 24 | "useESModules": false 25 | } 26 | ], 27 | "@babel/plugin-syntax-class-properties", 28 | ["import", { 29 | "libraryName": 30 | "antd", 31 | "libraryDirectory": "es", 32 | "style": true 33 | }] 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /example/demo/src/page/home/index.tsx: -------------------------------------------------------------------------------- 1 | //todo 2 | import React from 'react' 3 | import './index.scss' 4 | import { useCacheDispatch } from 'react-keepalive-router' 5 | 6 | class Index extends React.Component{ 7 | 8 | handerClick=(payload)=>{ 9 | const dispatch = useCacheDispatch() 10 | dispatch({ type:'reset' , payload }) 11 | } 12 | 13 | render(){ 14 | console.log(this.props) 15 | return
16 |
this.handerClick('/list')} 18 | >清除 生命周期
19 |
this.handerClick('/list2')} 21 | >清除 缓存列表
22 |
this.handerClick('/detail')} 24 | >清除 缓存表单
25 |
26 | } 27 | } 28 | 29 | 30 | 31 | 32 | 33 | export default Index -------------------------------------------------------------------------------- /example/demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | 15 |
16 | 17 | -------------------------------------------------------------------------------- /example/demo/src/page/input/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | 4 | 5 | 6 | class Index extends React.Component{ 7 | constructor(prop){ 8 | super(prop) 9 | this.state = { 10 | list: [ { id:1 , name: 'xixi' } ,{ id:2 , name: 'haha' },{ id:3 , name: 'heihei' } ], 11 | number:1 12 | } 13 | } 14 | render(){ 15 | const { number }:any = this.state 16 | return
17 | 18 |
19 |