@cutting/hooks

Cutting reusable hooks.

Usage no npm install needed!

<script type="module">
  import cuttingHooks from 'https://cdn.skypack.dev/@cutting/hooks';
</script>

README

@cutting/hooks - reusable react hooks for reusable things

npm version code style: prettier

install

pnpm add @cutting/hooks

# or

npm install @cutting/hooks

This package contains the following hooks:

useIsMounted

A React hook to let you know whether the component running the hook is still mounted.

usage

import { useIsMounted } from '../useIsMounted/useIsMounted';

const MyComp: FC = () => {
  const isMounted = useIsMounted();

   if (isMounted) {
    debouncedCallback(newSize);
  }

  // etc.
}

useScrollToTop

A React hook to bump the focus to the top of the viewport or to an optional ref

usage

import { useScrollToTop } from '@cutting/hooks';

const MyComp: FC = () => {
  const root = useRef<HTMLDivElement>(null);

  useScrollToTop({ ref: root });

  // etc.
}

usePrevious

usage

import {usePrevious} from '@cutting/hooks';

const Demo = () => {
  const [count, setCount] = React.useState(0);
  const prevCount = usePrevious(count);

  return (
    <p>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
      <p>
        Now: {count}, before: {prevCount}
      </p>
    </p>
  );
};