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

water-design-hooks

v1.0.1

Published

一个基于react的hooks库

Downloads

1

Readme

综述

water-design-hooks 是基于 react 的一套 hooks 库,用于 react 的状态函数组件开发

介绍

| 名称 | 说明 | 参数 | 版本 | | :----------------- | :---------------------------------------------- | :---------------------------------- | :----- | | useBoolean | 定义布尔值 | boolean | ^1.0.0 | | useFormInput | 对表单的valueonChange的复用封装 | string | ^1.0.0 | | useInterval | 计时器,按间隔执行回调函数,可控清除计时器 | - | ^1.0.0 | | useMousePosition | 获取鼠标位置 | - | ^1.0.0 | | useSyncState | 在调用 setState 后,可同步获取 state 改变后的值 | - | ^1.0.0 | | useScroll | 滚动的监听 | { container: function } | ^1.0.0 | | useRetry | 重试 | Function or AsyncFunction, number | ^1.0.1 |

使用

useBoolean

import React from 'react';
import { useBoolean } from 'water-design-hooks';

const App = () => {
  const [show, { setTrue, setFalse, toggle }] = useBoolean(true);

  return (
    <div>
      <button onClick={setTrue}>显示</button>
      <button onClick={setFalse}>隐藏</button>
      <button onClick={() => toggle()}>切换</button>
      <button onClick={() => toggle(true)}>切换显示</button>
      <button onClick={() => toggle(false)}>切换隐藏</button>
      <p>文本:</p>
      {show && <p>北京的金山上,光芒照四方</p>}
    </div>
  );
};

export default App;

useFormInput

import React from 'react';
import { useFormInput } from 'water-design-hooks';

const App = () => {
  const { value: value1, onChange: onChange1 } = useFormInput('1');
  const { value: value2, onChange: onChange2 } = useFormInput('2');
  // 抽成自定义的hook目的是不用在一个函数里声明多个value值,不用定义多个处理函数
  return (
    <>
      <input type="text" value={value1} onChange={onChange1} />
      <input type="text" value={value2} onChange={onChange2} />
    </>
  );
};

export default App;

useInterval

import React from 'react';
import { useInterval } from 'water-design-hooks';

const App = () => {
  const [count, setCount] = React.useState(0);
  const { clearInterval } = useInterval(() => {
    setCount(count + 1);
  }, 1000);

  return (
    <div>
      <button onClick={clearInterval}>关闭定时器</button>
      <p>当前数量: {count}</p>
    </div>
  );
};

export default App;

useMousePosition

import React from 'react';
import { useMousePosition } from 'water-design-hooks';

const App = () => {
  const { x, y } = useMousePosition();

  return (
    <h2>
      position: {x}, {y}
    </h2>
  );
};

export default App;

useSyncState

import React from 'react';
import { useSyncState } from 'water-design-hooks';

const App = () => {
  const [value1, setValue1] = React.useState(0);
  const [value2, { getValue: getValue2, setValue: setValue2 }] =
    useSyncState(0);

  const onValue1Click = () => {
    setValue1(value1 + 1);
    // 这里拿到的值还是更新前的值
    console.log(value1);
  };

  const onValue2Click = () => {
    setValue2(value2 + 1);
    // 这里拿到的值是更新后的值
    console.log(getValue2());
  };

  return (
    <div>
      <button onClick={onValue1Click}>value1自增</button>
      <span>{value1}</span>
      <hr></hr>
      <button onClick={onValue2Click}>value2自增</button>
      <span>{value2}</span>
    </div>
  );
};

export default App;

useScroll

import React from 'react';
import { useScroll } from 'water-design-hooks';

const App = () => {
  const ref = React.useRef(null);
  const { setPosition } = useScroll({
    // container: () => document.getElementById('wrapper'),
    container: () => ref.current,
  });

  return (
    <div
      style={{
        width: 300,
        height: 500,
        border: '2px solid #000',
        overflow: 'auto',
      }}
      // id="wrapper"
      ref={ref}
    >
      <div style={{ height: 1000, width: 700 }}>123</div>
      <span>456</span>
      <button
        style={{ marginLeft: 200 }}
        onClick={() =>
          setPosition({ top: 10 }, 600, () => {
            console.log(123);
          })
        }
      >
        回到顶部
      </button>
    </div>
  );
};

export default App;

useRetry

import React from 'react';
import { useRetry } from 'water-design-hooks';

let a = 0;
const fetchData = async () => {
  a++;
  return Promise.resolve({ name: '张三' + a });
};

const App = () => {
  const [data, setData] = React.useState<{ name?: string }[]>([]);
  useRetry(async () => {
    const res = await fetchData();
    setData((prev) => [...prev, res]);
    if (res.name === '张三10') {
      // 当返回true时结束该重试hook
      return true;
    }
  }, 1000);

  return (
    <div>
      {data.map((item, i) => {
        return <div key={i}>{item.name}</div>;
      })}
    </div>
  );
};

export default App;