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

@williamyi74/react-keepalive

v1.1.4

Published

基于react18+的缓存组件,拥有类似vue的KeepAlive组件功能效果

Downloads

30

Readme

react-keepalive · GitHub license npm version

a react cache component based on react@18+

Description

组件一共对外暴露了三个接口: KeepaliveScope组件,KeepAliveItem组件及hook函数useCacheDestroy

  1. 用KeepaliveScope组件包裹整个应用
  2. 用KeepaliveItem组件包裹你要缓存的组件给上缓存ID和Key 缓存ID和Key可以设置为一样且要唯一
  3. 被Keepalive组件包裹的组件会注入onActived和onUnActived生命周期钩子函数传入一个回调和缓存组件的cacheId 这个回调会在组件被激活和休眠时执行 下面会演示
  4. useCacheDestroy这个hook会返回一个销毁组件缓存的函数 传入cacheId即可销毁当前为休眠状态的缓存组件 但是不可销毁正处于激活状态的组件

Usage step

使用npm yarn pnpm安装到项目

npm i @williamyi74/react-keepalive
yarn add @williamyi74/react-keepalive
pnpm add @williamyi74/react-keepalive

首先用KeepAliveScope组件包裹应用 注意要被包裹在Router组件里面 否则路由hooks会用不了

index.tsx/main.tsx:

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import { BrowserRouter } from 'react-router-dom'
import { KeepAliveScope } from '@williamyi74/react-keepalive/es'
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <React.StrictMode>
    <BrowserRouter>
      <KeepAliveScope>
        <App />
      </KeepAliveScope>
    </BrowserRouter>
  </React.StrictMode>
)

一般都是用来包裹路由组件:

import { Route } from 'react-router-dom'
import { KeepAliveItem } from '@williamyi74/react-keepalive/es'
import List 'geek-pc/pages/list/list'
export const ListCacheId = 'KEEPALIVE_LIST'
const Layout = () => {
    return (
            <Route path="/">
                <KeepAliveItem cacheId={ListCacheId} key={ListCacheId}>
                    <List />
                </KeepAliveItem>
            </Route>
           )
}
export default Layout;

Page1组件演示被包裹的业务组件使用细节:

import { KeepAliveItem } from '@williamyi74/react-keepalive/es'
import { useCacheDestroy } from '@williamyi74/react-keepalive/es'
import { KeepAliveComponentProps } from '@williamyi74/react-keepalive/es/components/keepalive-item'
// 被KeepAliveItem组件包裹的业务组件Props会注入两个生命周期钩子函数和cacheId
// 编写被包裹的业务组件自身的业务props类型传入KeepAliveComponentProps
interface IProps {
    // ...
}
const Input = () => <input />
const InputCacheId = 'KEEPALIVE_INPUT'
const Page1 = ({
  onActived,
  onUnActived,
  cacheId,
}: KeepAliveComponentProps<IProps>) => {
  const cacheDestroy = useCacheDestroy() // 缓存销毁的hook
  // 组件加载后注册生命周期钩子函数
  useEffect(() => {
    onActived!(() => {
      console.log('Page1组件激活了')
    }, cacheId!)

    onUnActived!(() => {
      console.log('Page1组件休眠了')
    }, cacheId!)
  }, [])
  const [show, setShow] = useState(false)
  return (
    <>
      <h2>page1</h2>
      {show && (
        <KeepAliveItem key={InputCacheId} cacheId={InputCacheId}>
        {/* 缓存非路由组件的自定义组件 不要直接写原生html元素 用一个自定义组件封装起来 */}
          <Input /> 
        </KeepAliveItem>
      )}
      <button
        onClick={() => {
          cacheDestroy('page2') // 销毁cacheId为page2的缓存组件 注意缓存组件里嵌套的缓存组件无法销毁 因为外层缓存组件包含了嵌套的缓存组件元素 所以一样还是会被缓存
        }}
      >
        清除缓存
      </button>
      <button onClick={() => setShow((prev) => !prev)}>
        {show ? '隐藏' : '显示'}
      </button>
    </>
  )
}

routes.tsx文件:

import { RouteObject } from 'react-router-dom'
import { KeepAliveItem } from '@williamyi74/react-keepalive/es'
import Page1 from '@/pages/keep-alive-practice/page-1/page-1'
export const Page1CacheId = 'KEEPALIVE_PAGE1'
const routes:RouteObject[] = [
  {
    path: '/page1',
    element: (
      <KeepAliveItem cacheId={Page1CacheId} key={Page1CacheId}>
        <Page1 />
      </KeepAliveItem>
    ),
  }
]
export default routes

容器组件里渲染:

import { useRoutes } from 'react-router-dom'
import routes from '@/routes'
const Layout = () => {
    return useRoutes(routes)
}
export default Layout