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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-halt

v1.5.0

Published

react中实现组件的冻结

Readme

React-Halt

一个用于实现 React 组件"冻结"效果的轻量级库。

介绍

React-Halt 提供了一种优雅的方式来"冻结"React 组件,类似于 Vue 中的 keep-alive 功能。当组件被设置为"冻结"状态时,其内部状态会被保留,但不会进行重新渲染,从而提高应用性能并保持组件状态。

安装

// use npm
npm install react-halt
// use yarn
yarn add react-halt

使用方法

基础用法

import { ReactHalt } from 'react-halt';
import { useState } from 'react';

function App() {
  const [stasis, setStasis] = useState(false);

  return (
    <div>
      <button onClick={() => setStasis(!stasis)}>
        {stasis ? '激活组件' : '冻结组件'}
      </button>

      <ReactHalt stasis={stasis}>
        <YourComponent />
      </ReactHalt>
    </div>
  );
}

使用生命周期钩子

import { ReactHalt } from 'react-halt';
import { useState } from 'react';

function App() {
  const [stasis, setStasis] = useState(false);

  const handleActivate = () => {
    console.log('组件被激活');
  };

  const handleDeactivate = () => {
    console.log('组件被冻结');
  };

  return (
    <div>
      <button onClick={() => setStasis(!stasis)}>
        {stasis ? '激活组件' : '冻结组件'}
      </button>

      <ReactHalt
        stasis={stasis}
        onActivate={handleActivate}
        onDeactivate={handleDeactivate}
      >
        <YourComponent />
      </ReactHalt>
    </div>
  );
}

API

ReactHalt

| 属性 | 类型 | 必填 | 描述 | | --- | --- | --- | --- | | stasis | boolean | 是 | 控制组件是否处于"冻结"状态 | | children | React.ReactNode | 是 | 需要被"冻结"管理的子组件 | | onActivate | () => void | 否 | 当组件从"冻结"状态变为激活状态时的回调函数 | | onDeactivate | () => void | 否 | 当组件从激活状态变为"冻结"状态时的回调函数 |

工作原理

React-Halt 利用 React 的 Suspense 和一个特殊的无限 Promise 机制来实现组件的"冻结"效果。当 stasis 属性设置为 true 时,组件会被挂起但不会卸载,从而保持其内部状态。

许可证

MIT