0.4.0 • Published 4 years ago

@jimengio/jimo-hooks v0.4.0

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

Jimo React Hooks

Collection of some react hooks

CI npm GitHub license

Usage

Installation

yarn add @jimengio/jimo-hooks

Hooks

useAsyncClick

Click event with loading

optiontypedefaultexplain
asyncFuncfunctionasync function
initStateObject{ loading: false }initial loading
returntypedefaultexplain
callbackfunction
loadingboolean
errorErrorcatch error
import { useAsyncClick } from "@jimengio/jimo-hooks";

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

const Demo = ({ asyncFn }) => {
  const { callback, loading } = useAsyncClick(asyncFn);

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

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 "@jimengio/jimo-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 "@jimengio/jimo-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
delaynumber0useDebouncedCallbackArgs"delay"
optionsObjectuseDebouncedCallbackArgs"options"
returntypedefaultexplain
callbackfunction
loadingboolean
cancelDebouncedCallbackfunctionuseDebouncedCallbackReturns"cancelDebouncedCallback"
callPendingfunctionuseDebouncedCallbackReturns"callPending"
errorErrorcatch error
import { useDebouncedClick } from "@jimengio/jimo-hooks";

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

const Demo = ({ asyncFn }) => {
  const { callback, loading } = useDebounceClick(asyncFn);

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

useDeepCompareCache

optiontypedefaultexplain
valueany
import { useDeepCompareCache } from "@jimengio/jimo-hooks";

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

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

  // ...
};
import { useDeepCompareCache } from "@jimengio/jimo-hooks";

const Demo = () => {
  // Deep comparison React.useEffect
  useEffect(() => {
    // ...
  }, useDeepCompareCache([A, B]));

  // Deep comparison React.useCallback
  const callback = useCallback(() => {
    // ...
  }, useDeepCompareCache([A, B]));

  // ...
};

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 "@jimengio/jimo-hooks";

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

  // ...
};

useInterval

Handle the setInterval timer function.

optiontypedefaultexplain
fnfunctionHandle function. (setInterval callback)
delaynumber0setInterval ms.
returntypedefaultexplain
stateidle, runningidleOperating status.
cancelfunctionThe clear timer function.
runfunctionManual restart interval function.
import { useInterval } from "@jimengio/jimo-hooks";

const Demo = () => {
  const { state, cancel, run } = useInterval(() => {
    console.log("hi");
  }, 1000);

  // ...
};

useMountedState

Check component mount state

optiontypedefaultexplain
----
returntypedefaultexplain
callbackfunctionGet mount status
import { useMountedState } from "@jimengio/jimo-hooks";

const Demo = () => {
  const getMounted = useMountedState();

  useEffect(() => {
    setTimeout(() => {
      if (getMounted()) {
        // do...
      }
    }, 1000);
  }, []);

  // ...
};

useLoadImg

Get image loading status

optiontypedefaultexplain
srcstring<img /> src
reqLoadingbooleanrequest loading
classNamestring
styleObject
imgPropsObject<img /> props
returntypedefaultexplain
imgNodeJSX.Element<img />
stateloading, done, error, idleidleimage state
loadingboolean
isErrorbooleanimage errored
import { useLoadImg } from "@jimengio/jimo-hooks";

const Demo = () => {
  const { imgNode, loading } = useLoadImg({
    src: "[PATH]/demo.jpg",
    style: { width: "100%" },
  });

  return <div data-loading={loading}>{imgNode}</div>;
};

useThrottle

throttled value

returntypeexplain
valueanyReturns the new throttled value.
import { useThrottle } from "@jimengio/jimo-hooks";

const Demo = ({ value }) => {
  const tValue = useThrottle(value);

  // ...
};

useThrottleFn

throttled function

returntypeexplain
callbackfunctionThe new throttled function.
cancelfunctionThe clear timer function.
callPendingfunctionThe callback manually function.
import { useThrottleFn } from "@jimengio/jimo-hooks";

const Demo = () => {
  const { callback, cancel } = useThrottleFn(() => {
    console.log("click");
  });

  return <button onClick={callback}>++</button>;
};

useUnmount

Unmount callback

optiontypedefaultexplain
fnfunction
import { useUnmount } from "@jimengio/jimo-hooks";

const Demo = () => {
  useUnmount(() => {
    console.log("Unmount");
  });

  // ...
};

useUpdateEffect

React.useEffect cancel the first mount trigger

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

const Demo = () => {
  useUpdateEffect(() => {
    console.log(value);
  }, [value]);

  // Deep comparison useUpdateEffect
  useUpdateEffect(() => {
    console.log(value);
  }, useDeepCompareCache([value]));

  // ...
};

Dependencies

Dev

# build package
yarn build

# tests
yarn test

# lint
yarn lint

License

MIT

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.0-alpha

4 years ago

0.1.0

4 years ago

0.1.0-alpha

4 years ago