0.1.0-alpha • Published 4 years ago

@jimengio/hooks v0.1.0-alpha

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React Hooks

Collection of some react hooks

CI npm GitHub license

Usage

Installation

yarn add @react-cmpt/hooks

Hooks

useAsyncClick

Click event with loading

optiontypedefaultexplain
asyncFuncfunctionasync function
import { useAsyncClick } from "@react-cmpt/hooks";

const asyncFn = async () => {
  // do something
};

const Demo = () => {
  const [clickEvent, loading] = useAsyncClick(asyncFn);

  return <Button loading={loading} click={clickEvent} />;
};

useDebounce

optiontypedefaultexplain
valueany
delaynumber
optionsObject

options:

optiontypedefaultexplain
maxWaitnumberDescribes the maximum time func is allowed to be delayed before it's invoked
leadingbooleanThis param will execute the function once immediately when called. Subsequent calls will be debounced until the timeout expires.
equalityFnfunctionComparator function which shows if timeout should be started

more options

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

const Demo = () => {
  const [text, setText] = useState("hello");
  const [value] = useDebounce(text, 1000);

  return (
    <div>
      <input
        defaultValue={"hello"}
        onChange={e => {
          setText(e.target.value);
        }}
      />
      <p>Actual value: {text}</p>
      <p>Debounce value: {value}</p>
    </div>
  );
};

useDebouncedCallback

optiontypedefaultexplain
callbackfunction
delaynumber
optionsObject
import { useDebouncedCallback } from "@react-cmpt/hooks";

const Demo = () => {
  const [value, setValue] = useState();
  // Debounce callback
  const [debouncedCallback] = useDebouncedCallback(
    // function
    value => {
      setValue(value);
    },
    // delay in ms
    1000
  );

  return (
    <div>
      <input onChange={e => debouncedCallback(e.target.value)} />
      <p>Debounced value: {value}</p>
    </div>
  );
};

useDebouncedClick

Click event with loading and debounce

optiontypedefaultexplain
asyncFuncfunctionasync function
delaynumber300useDebouncedCallbackArgs"delay"
optionsObjectuseDebouncedCallbackArgs"options"
import { useDebouncedClick } from "@react-cmpt/hooks";

const asyncFn = async () => {
  // do something
};

const Demo = () => {
  const [clickEvent, loading] = useDebounceClick(asyncFn);

  return <Button loading={loading} click={clickEvent} />;
};

useDeepCompareCache

optiontypedefaultexplain
valueany
import { useDeepCompareCache } from "@react-cmpt/hooks";

const obj1 = { a: 1, b: { b1: 2 } };
const obj2 = { a: 1, b: { b1: 2 } };

const Demo = () => {
  const obj = useDeepCompareCache(obj1);
  console.log(obj === obj1); // true
  console.log(obj === obj2); // true

  // ...
};

useDeepEffect

Deep comparison React.useEffect

optiontypedefaultexplain
effectfunctionImperative function that can return a cleanup function
depsArrayIf present, effect will only activate if the values in the list change.
import { useDeepEffect } from "@react-cmpt/hooks";

const Demo = ({ value: Object }) => {
  useDeepEffect(() => {
    // do something
  }, [value]);

  // ...
};

Dependencies

Dev

# build package
yarn build

# tests
yarn test

# lint
yarn lint

License

MIT