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

@damony/react-keep-alive

v1.0.5

Published

react-keep-alive component

Downloads

306

Readme

react-keep-alive

react-keep-alive 组件库,可用于简单项目缓存路由,即跳转路由时不卸载路由组件,跳转回来时保持跳转前的状态。

使用要求

需使用react-router@6,且你的路由组件基于同一个根组件或layout组件。

安装

pnpm add @damony/react-keep-alive

使用

使用 KeepAlive 包裹根组件,needKeepAlivePaths即需要缓存的路由。

// src/main.tsx
import { KeepAlive } from '@damony/react-keep-alive';

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <KeepAlive needKeepAlivePaths={['/list']}>
      // your router
      <RouterProvider router={router} />
    </KeepAlive>
  </StrictMode>
);

使用 KeepAliveOutlet 代替 Outlet。

import { KeepAliveOutlet } from '@damony/react-keep-alive';

export default function App() {
  return (
    ...
    // <Outlet />
    <KeepAliveOutlet />
  )
}

在被缓存的组件中使用useKeepAliveTargetPaths(['targetPath']),这样可以仅在跳转至目标路由时才缓存,跳转其他路由时正常卸载。例如从列表跳转至详情时,在列表组件中使用useKeepAliveTargetPaths

useOnBack可在跳转回缓存的路由时执行某些操作,例如刷新列表。

import { useOnBack } from '@damony/react-keep-alive';

export default function List(){
  useOnBack(()=>{
    // do something
  })
  return (...)
}