npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

awesome-react-handy-hooks

v0.0.1

Published

Awesome React Handy Hooks is a collection of custom hooks that can be used in your React applications.

Downloads

10

Readme

awesome-react-handy-hooks

License npm version npm downloads GitHub issues GitHub stars

Description

The awesome-react-handy-hooks package is a collection of handy React hooks that provide additional functionality and simplify common tasks in React applications. These hooks are designed to enhance productivity and improve code readability by abstracting complex logic into reusable and composable units. With awesome-react-handy-hooks, developers can easily handle state management, side effects, and more, allowing them to build robust and efficient React applications with ease.

Installation

You can install the package using npm:

$ npm install awesome-react-handy-hooks

Table of Contents

Hooks

useCounter

The useCounter hook is used to manage a counter state and its functions to increment and decrement it.

Usage:

const { count, increment, decrement } = useCounter(0);

return (
  <div>
    <p>Count: {count}</p>
    <button onClick={increment}>Increment</button>
    <button onClick={decrement}>Decrement</button>
  </div>
);

useToggle

The useToggle hook is used to manage a toggle state and its function to change it.

Usage:

const { isToggled, toggle } = useToggle();

return (
  <div>
    <p>Toggle: {isToggled ? "On" : "Off"}</p>
    <button onClick={toggle}>Toggle</button>
  </div>
);

useDialog

The useDialog hook is used to manage a dialog state and its functions to open and close it.

Usage:

const { isOpen, open, close } = useDialog();

return (
  <div>
    <button onClick={open}>Open Dialog</button>
    {isOpen && (
      <div>
        <p>This is a dialog</p>
        <button onClick={close}>Close Dialog</button>
      </div>
    )}
  </div>
);

useClickOutside

The useClickOutside hook is used to check if the click is outside an element and call a function when it is.

Usage:

const elementRef = useRef(null);

useClickOutside(ref, () => {
  console.log("call when the click is outside the elementRef");
});

return <div ref={elementRef}>My Component</div>;

useHover

The useHover hook is used to check if the hover is inside an element and call a function when it is.

Usage:

const elementRef = useRef(null);

useHover(ref, () => {
  console.log("call when the hover is inside the elementRef");
});

return <div ref={elementRef}>My Component</div>;

useFocus

The useFocus hook is used to check if the focus is inside an element and call a function when it is.

Usage:

const elementRef = useRef(null);

useFocus(ref, () => {
  console.log("call when the focus is inside the elementRef");
});

return <input ref={elementRef} value="Hello react" />;

useMediaQuery

The useMediaQuery hook is used to check if a media query matches.

Usage:

const isMobile = useMediaQuery("(max-width: 768px)");

useMousePosition

The useMousePosition hook is used to get the current mouse position.

Usage:

const { x, y } = useMousePosition();

useCopyToClipboard

The useCopyToClipboard hook is used to copy a string to the clipboard.

Usage:

const { copied, copyToClipboard } = useCopyToClipboard();

useDebounce

The useDebounce hook is used to debounce a value.

Usage:

const debouncedValue = useDebounce(value, 1000);

useThrottle

The useThrottle hook is used to throttle a value.

Usage:

const throttledValue = useThrottle(value, 1000);

useMount

The useMount hook is used to execute a function on mount.

Usage:

const callOnMount = () => {
  console.log("Call on mount");
};

useMount(callOnMount);

useUnmount

The useUnmount hook is used to execute a function on unmount.

Usage:

const callOnUnmount = () => {
  console.log("Call on unmount");
};

useUnmount(callOnUnmount);

useUpdateEffect

The useUpdateEffect hook is used to execute a function on update when the dependencies changes.

Usage:

useUpdateEffect(() => {
  console.log("Component updated");
}, [dep1, dep2]);

useEventListener

The useEventListener hook is used to listen for events on an element and call a function when the event is triggered.

Usage:

useEventListener(
  "click",
  () => {
    console.log("The document was clicked");
  },
  document
);

useWindowSize

The useWindowSize hook is used to get the width and height of the window and update it on resize.

Usage:

const size = useWindowSize();

useOnline

The useOnline hook is used to check if the user is online.

Usage:

const online = useOnline();

useScript

The useScript hook is used to load a script and manage its loading status.

Usage:

const loading = useScript("https://example.com/script.js");

useStyle

The useStyle hook is used to load a stylesheet and manage its loading status.

Usage:

const loading = useStyle("https://example.com/style.css");

useTitle

The useTitle hook is used to change the title of the document.

Usage:

useTitle("New Title");

useFetch

The useFetch hook is used to fetch data from an API endpoint and manage the loading, data, and error states.

Usage:

const { data, loading, error } = useFetch("https://example.com/api");

useLongPress

The useLongPress hook is used to execute a function on long press.

Usage:

const longPressEvent = useLongPress(() => {
  console.log("Long press event");
}, 300);

return (
  <div>
    <button {...longPressEvent}>Press and hold me</button>
  </div>
);

useKeyPress

The useKeyPress hook is used to detect if a key is pressed.

Usage:

const isKeyPressed = useKeyPress("a");

useScroll

The useScroll hook is used to get the scroll position.

Usage:

const { x, y } = useScroll();

useBase64Encode

The useBase64Encode hook is used to encode a string to base64.

Usage:

const { encoded, encode } = useBase64Encode();

useBase64Decode

The useBase64Decode hook is used to decode a base64 string.

Usage:

const { decoded, decode } = useBase64Decode();

useDeviceMotion

The useBase64Decode hook is used to decode a base64 string.

Usage:

const { decoded, decode } = useBase64Decode();

useDeviceOrientation

The useDeviceMotion hook is used to get the device motion.

Usage:

const { x, y, z } = useDeviceMotion();

useGeolocation

The useGeolocation hook is used to get the geolocation of the user.

Usage:

const { latitude, longitude } = useGeolocation();

useIdle

The useIdle hook is used to check if the user is idle.

Usage:

const isIdle = useIdle(1000);

useIntersectionObserver

The useIntersectionObserver hook is used to observe an element's intersection details.

Usage:

const elementRef = useRef(null);

const options = {
  root: null,
  rootMargin: "0px",
  threshold: 1.0,
};

const intersection = useIntersectionObserver(elementRef, options);

usePageLeave

The usePageLeave hook is used to execute a function when the user leaves the page.

Usage:

usePageLeave(() => {
  console.log("The user left the page");
});