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

@developer-bug/custom-hooks

v1.1.4

Published

This project is designed to be a centralized and well-organized repository of custom React hooks that can be reused in future projects. This repository not only facilitates code reuse but also promotes consistency and efficiency in React application devel

Downloads

416

Readme

Custom Hooks

This project is designed to be a centralized and well-organized repository of custom React hooks that can be reused in future projects. This repository not only facilitates code reuse but also promotes consistency and efficiency in React application development.

Project Features

  1. Modular Structure: Each hook is contained within its own module, with accompanying documentation and usage examples.
  2. Types of Hooks: The repository includes hooks for managing state, effects, contexts, events, and other common React patterns.
  3. Compatibility: Hooks are designed to be compatible with the latest versions of React.
  4. Documentation: Each hook has internal documentation that explains in detail how it works and provides examples for its use as well.
  5. Testing: Implements unit tests to ensure the functionality and stability of the hooks.
  6. Continuous Updates: The project is kept up-to-date with new contributions and improvements.

Installation

npm install @developer-bug/custom-hooks

Hooks

useDebounce

Custom hook to debounce a value, delaying updates to the value until after a specified delay has passed since the last change.

How to use:

import { useDebounce } = from '@developer-bug/custom-hooks';

const SearchComponent = () => {
    const [searchTerm, setSearchTerm] = useState('');
    const debouncedSearchTerm = useDebounce(searchTerm, 500);

    useEffect(() => {
        if (debouncedSearchTerm) {
            // Fetch data only when the debounced search term changes
            fetchData(debouncedSearchTerm);
        }
    }, [debouncedSearchTerm]);

    return (
        <input
            type="text"
            placeholder="Search..."
            value={searchTerm}
            onChange={(e) => setSearchTerm(e.target.value)} />
    );
};

useDocumentTitle

Custom hook to set the document title in a React component.

How to use:

import { useDocumentTitle } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    useDocumentTitle("My Page Title");
    return <div>Check the document title!</div>;
};

useElementDetector

Custom hook to detect element visibility within the viewport using IntersectionObserver.

How to use

import { useRef } from 'react';
import { useElementDetector } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const ref = useRef<HTMLDivElement>(null);
    const isVisible = useElementDetector(
        ref,
        {
            threshold: 0.5, rootMargin: -10
        },
        {
            onTriggerEnter: () => console.log("Entered viewport"),
            onFirstVisible: () => console.log("Visible for the first time"),
            onTriggerExit: () => console.log("Exited viewport"),
            onChangeVisibility: (isVisible) => console.log(`Visibility changed: ${isVisible}`)
        }
    );
    return <div ref={ref}>Check if I am visible</div>;
};

useFetch

Custom hook for making HTTP requests using the Fetch API in React. This hook provides methods for all standard HTTP methods and can handle GET, POST, PUT, and DELETE requests.

How to use:

  • GET Request

All Data

import { useFetch } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const { data, error, loading, get } = useFetch<ResposeType>('https://api.example.com/data');

    useEffect(() => {
        get();
    }, []);

    reutrn(
        <ul>
            {data && data.map((item, index) => (
                <li key="{item.exampleId}">
                    {item.exampleContent}
                </li>
            ))}
        </ul>
    );
}

One Data

import { useFetch } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const { data, error, loading, get } = useFetch<ResposeType>('https://api.example.com/data/1');

    useEffect(() => {
        get();
    }, []);

    reutrn(
        <div>
            <h1>{data.exampleTitle}</h1>
            <p>{data.exampleBody}</p>
        </div>
    );
}
  • POST Request
import { useFetch } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const { data, error, loading, post } = useFetch<RequestResposeType, RequestBodyType>('https://api.example.com/data');

    const handleSubmit = async (newData: RequestBodyType) => {
        await post(newData);
    };

    reutrn(
        <button onClick={handleSubmit}>
            New Data
        </button>
    );
}
  • PUT Request
import { useFetch } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const { data, error, loading, put } = useFetch<RequestResposeType, RequestBodyType>('https://api.example.com/data/1');

    const handleSubmit = async (updateData: RequestBodyType) => {
        await put(updateData);
    };

    reutrn(
        <button onClick={handleSubmit}>
            Update Data
        </button>
    );
}
  • DELETE Request
import { useFetch } = from '@developer-bug/custom-hooks';

const MyComponent = () => {
    const { data, error, loading, delete: deleteRequest } = useFetch<RequestResposeType, RequestBodyType>('https://api.example.com/data/1');

    reutrn(
        <button onClick={deleteRequest}>
            Delete Data
        </button>
    );
}

Contributing

If you'd like to contribute to Custom Hooks, here are some guidelines:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your changes.
  4. Write tests to cover your changes.
  5. Run the tests to ensure they pass.
  6. Commit your changes.
  7. Push your changes to your forked repository.
  8. Submit a pull request to dev branch.

Authors and Acknowledgment

Custom Hooks was created by JessyAvalosB

Additional contributors include:

Thank you to all the contributors for their hard work and dedication to the project.

Feedback

[email protected]