@mango-libs/react-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Englist | 中文

npm npm GitHub forks GitHub Repo stars

Installation

npm install @mango-libs/react-hooks

Usage

import { useXXX } from "@mango-libs/react-hooks";

API

1. useDebounce

useDebounce(func: Function, delay: number = 500): { run: Function, cancel: Function }

eg.

import { useDebounce } from "@mango-libs/react-hooks";

function App() {
  const { run: handleChange } = useDebounce(() => {
    console.log("useDebounce test.");
  }, 500);

  return <input onChange={handleChange} />;
}
2. useThrottle

useThrottle(func: Function, delay: number = 500): { run: Function, cancel: Function }

eg.

import { useThrottle } from "@mango-libs/react-hooks";

function App() {
  const { run: handleClick } = useThrottle(() => {
    console.log("useThrottle test.");
  }, 500);

  return <button onClick={handleClick}>continuous click</button>;
}
3. useCountdown
useCountdown({
  duration: number; // ms
  interval?: number; // ms (default: 1000)
  onFinish?: () => void;
}): {
  remaining: number; // ms
  start: () => void;
  pause: () => void;
  reset: () => void;
  stop: () => void;
}; // return

eg.

import { useCountDown } from "@mango-libs/react-hooks";

function App() {
  const { start, pause, reset, stop, remaining } = useCountDown({
    duration: 10 * 1000,
    onFinish: () => {
      console.log("finish");
    },
  });

  return (
    <div>
      <button onClick={start}>start</button>
      <button onClick={pause}>pause</button>
      <button onClick={reset}>reset</button>
      <button onClick={stop}>stop</button>
      <div>{`${Math.ceil(remaining / 1000)} sec`}</div>
    </div>
  );
}
4. useCookie
useCookie(): [object, SetCookieFunc, RemoveCookieFunc, ClearCookieFunc];
useCookie(name: string): [object, SetCookieFunc, RemoveCookieFunc, ClearCookieFunc];
useCookie(names: string[]): [object, SetCookieFunc, RemoveCookieFunc, ClearCookieFunc];

eg.

import { useCookie } from "@mango-libs/react-hooks";

function App() {
  const [cookie, setCookie, removeCookie, clearCookie] = useCookie();
  return (
    <div>
      <button onClick={() => setCookie("foo", "bar")}>set</button>
      <button onClick={() => removeCookie("foo")}>remove</button>
      <button onClick={() => clearCookie()}>clear</button>
      <div>{cookie.name}</div>
    </div>
  );
}
5. useCookieValue

useCookieValue(name: string): [string, SetCookieFunc, RemoveCookieFunc];

eg.

import { useCookieValue } from "@mango-libs/react-hooks";

function App() {
  const [value, setCookie, removeCookie] = useCookieValue("foo");
  return (
    <div>
      <button onClick={() => setCookie("bar")}>set</button>
      <button onClick={() => removeCookie()}>remove</button>
      <div>{value}</div>
    </div>
  );
}
6. useLocalStorage
// Same as useCookie.
7. useLocalStorageValue
// Same as useCookieValue.
8. useSessionStorage
// Same as useCookie.
9. useSessionStorageValue
// Same as useCookieValue.

Package Sidebar

Install

npm i @mango-libs/react-hooks

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

30.4 kB

Total Files

23

Last publish

Collaborators

  • chutao.zhang