@yuxuan-zheng/hooks
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

hooks

Npm package version

Some common used hooks.

API Reference

All hooks will return either a value or an array. If the return value is an array, this document will list the values in order.

useDebounced

Description

Use this hook to get the debounced value.

Parameters

Name Type Default value Description
value T undefined Required The value you want to debounce
interval number 1000 The debounce interval in milliseconds

Returns

Name Type Description
debouncedValue T The debounced value

Example

const Example = () => {
  const [value, setValue] = useState('');
  const debouncedValue = useDebounced(value);

  const onChange = e => setValue(e.target.value);

  useEffect(() => {
    // Do whatever you want upon the debounced value change
    console.log(debouncedValue);
  }, [debouncedValue]);

  return <input type="text" value={value} onChange={onChange} />;
};

useIntersection

Description

Use this hook to determine whether the element is displayed in viewport.

Parameters

This hook doesn't need parameters.

Returns

Name Type Description
isIntersecting boolean Use this value to determine whether the target element is intersecting with viewport.
callbackRef (element:HTMLElement) => void Pass this callback to the target element's ref.

Example

const Example = () => {
  const [isIntersecting, callbackRef] = useIntersection();

  useEffect(() => {
    // Do whatever you want when the target is intersecting with viewport
  }, [isIntersecting])

  return <div ref={callbackRef}></div>
}

Readme

Keywords

Package Sidebar

Install

npm i @yuxuan-zheng/hooks

Weekly Downloads

6

Version

0.0.3

License

MIT

Unpacked Size

23.8 kB

Total Files

14

Last publish

Collaborators

  • yuxuan-zheng