npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-cache-router

v1.1.0

Published

实现react-router的keep-alive功能

Downloads

33

Readme

react-cache-router

基于reactReactVersion实现的路由缓存功能

搭配react-router-domreactRouterVersion以上进行工作

安装

# 使用npm安装
npm install react-cache-router --save
# 或者使用yarn
yarn add react-cache-router

为何实现

在使用vue-router时,只要使用keep-alive组件包裹,便能将在路由发生改变时,将组件状态记录下来。

而在使用react-router时,发现react并没keep-alive组件,route也没有提供缓存组件状态的方法,导致每次路由回退时,当前路由下的数据和滚动位置丢失。

react-cache-router就是为了实现react-routerkeep-alive功能。

DEMO演示

实现原理

我们利用了react-router中,无论当前路由是否匹配,children永远不会被卸载的原理。

react-router源码

使用方法

提供了CacheSwitchCacheRoute,只要替换掉原本的react-router-dom提供的SwitchRoute即可使用。

CacheSwitchCacheRoute是互相依赖,没办法单独使用。

import * as React from 'react';
import {HashRouter} from 'react-router-dom';
import {CacheSwitch, CacheRoute} from 'react-cache-router';
import {Home} from "./pages/home";
import {Myself} from "./pages/myself"

export class App extends React.Component{
    render(){
        return <HashRouter>
            <CacheSwitch>
                <CacheRoute exact={true} path="/home" component={Home}/>
                <CacheRoute exact={true} path="/myself" component={Myself}/>
            </CacheSwitch>
        </HashRouter>;
    }
}

参数

CacheSwitch

| 参数名称 | 类型 | 默认值 | 具体描述 | | ----- | ----- | ----- | ----- | | when | "forward" "back" "all" "none" | "forward" | 表示何时才缓存路由,forward表示路由前进(PUSH or REPLACE)时缓存,back表示路由后退(POP)时缓存,all无论前进和后退均缓存(即不主动卸载缓存,但超出最大缓存路由数还是会卸载路由),none即不缓存。 | | maxRoutes | Number | -1 | 最多缓存几条路由,默认-1,即无缓存数量限制,超过数量后会优先卸载最后使用到的路由 |

CacheRoute

cacheRoute 继承Route的所有参数,并添加了以下的参数

| 参数名称 | 类型 | 默认值 | 具体描述 | | ----- | ----- | ----- | ----- | | cache | Boolean | true | 设置当前路由是否需要缓存,默认需要缓存。 | | local | Boolean | false | 本地持久缓存,仅当cache为true时生效,不受CacheSwitch的maxRoutes和when为forward和back的影响。 |