
<a href="https://www.npmjs.com/package/@custom-react-hooks/all">
<img src="https://img.shields.io/npm/v/@custom-react-hooks/all.svg"
</a>
<a href="https://github.com/djkepa/custom-react-hooks/blob/main/LICENSE">
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License" />
</a>
<a href="https://github.com/djkepa/custom-react-hooks/stargazers">
<img src="https://img.shields.io/github/stars/djkepa/custom-react-hooks.svg" alt="GitHub Stars" />
</a>
<a href="https://github.com/djkepa/custom-react-hooks/network">
<img src="https://img.shields.io/github/forks/djkepa/custom-react-hooks.svg" alt="GitHub Forks" />
</a>
<a href="https://github.com/djkepa/custom-react-hooks/issues">
<img src="https://img.shields.io/github/issues/djkepa/custom-react-hooks.svg" alt="GitHub Issues" />
</a>
A collection of reusable and well-documented custom React hooks for supercharging your React applications. These hooks cover a wide range of functionalities, making it easier for you to build dynamic and interactive user interfaces.
https://custom-react-hooks-live.vercel.app
Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.
If you prefer to have all hooks at your disposal, you can install the entire collection:
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
Each hook is a standalone package, and you can install them individually using npm or yarn:
npm install @custom-react-hooks/use-async
or
yarn add @custom-react-hooks/use-async
The useExample hook must be imported using a named import as shown below:
Named Import:
import { useExample } from '@custom-react-hooks/use-example';
This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.
The useAsync hook simplifies the handling of asynchronous operations in React applications, such as data fetching or any other promise-returning functions. It provides a structured way to track the status and outcome of async operations.
Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.
npm install @custom-react-hooks/use-async
or
yarn add @custom-react-hooks/use-async
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
import React, { useState } from 'react';
import { useAsync } from '@custom-react-hooks/all';
const fetchData = async (endpoint) => {
const response = await fetch(endpoint);
if (!response.ok) {
throw new Error(`Failed to fetch from ${endpoint}`);
}
return response.json();
};
const AsyncComponent = () => {
const [endpoint, setEndpoint] = useState('');
const [simulateError, setSimulateError] = useState(false);
const { execute, status, value: data, error } = useAsync(() => fetchData(endpoint), false);
const handleFetch = () => {
if (simulateError) {
setEndpoint('https://jsonplaceholder.typicode.com/todos/1');
}
execute();
};
return (
<input
type="text"
value={endpoint}
onChange={(e) => setEndpoint(e.target.value)}
placeholder="Enter API endpoint"
/>
<button onClick={handleFetch}>Fetch Data</button>
<label>
<input
type="checkbox"
checked={simulateError}
onChange={() => setSimulateError(!simulateError)}
/>
Simulate Error
</label>
{status === 'pending' &&
Loading...
}
{status === 'error' &&
Error: {error?.message}
}
{status === 'success' && (
<h3>Data:</h3>
<pre>{JSON.stringify(data, null, 2)}</pre>
)}
);
};
export default AsyncComponent;
In this example, the useAsync hook is used to perform an asynchronous data fetch operation.
asyncFunction (Function): The asynchronous function to execute.immediate (Boolean, optional): A boolean indicating whether the async function should be executed immediately on component mount. Defaults to false.An object with the following properties:
execute (Function): A function to trigger the async operation.status (String): The current status of the async operation (idle, pending, success, error).value (Any): The value returned from the async operation.error (Error | null): Any error that occurred during the execution.API Data Fetching: Fetching data from an API when a component mounts or based on user actions.
Form Submission Handling: Managing asynchronous form submissions to a server, including loading states and error handling.
Lazy Loading: Dynamically loading components or data based on certain conditions or user interactions.
Web API Interactions: Simplifying the use of asynchronous Web APIs (like geolocation or camera access).
File Uploads: Handling the asynchronous process of file uploads, including progress tracking and error management.
Real-time Data Updates: Managing WebSocket connections or server polling for live data updates.
Complex Calculations/Processing: Executing and managing state for asynchronous complex calculations, such as those using Web Workers.
Third-party Service Integration: Facilitating interactions with asynchronous third-party services (e.g., payment gateways, social media APIs).
Conditional Async Operations: Executing asynchronous tasks based on specific conditions or inputs.
Sequencing Async Operations: Coordinating multiple dependent asynchronous operations in sequence.
Contributions to enhance useAsync are highly encouraged. Feel free to submit issues or pull requests to the repository.
The useClickOutside hook is designed to detect and handle clicks outside of a specified element or set of elements. This is particularly useful for closing modal windows, dropdowns, and other components when a user interacts outside of them.
mousedown and touchstart to determine outside clicks, with the option to customize the events.Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.
npm install @custom-react-hooks/use-click-outside
or
yarn add @custom-react-hooks/use-click-outside
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
Here's an example of how to use the useClickOutside hook in a modal component:
import React, { useState, useRef } from 'react';
import { useClickOutside } from '@custom-react-hooks/all';
const Modal = ({ onClose }) => {
const modalRef = useRef(null);
useClickOutside(modalRef, onClose);
return (
Modal content goes here
<button onClick={onClose}>Close Modal</button>
);
};
const ClickOutsideComponent = () => {
const [isModalOpen, setModalOpen] = useState(false);
const openModal = () => setModalOpen(true);
const closeModal = () => setModalOpen(false);
return (
<button onClick={openModal}>Open Modal</button>
{isModalOpen && <Modal onClose={closeModal} />}
);
};
export default ClickOutsideComponent;
In the above example, clicking outside the `
containing the modal content will trigger theonClose` function.
refs (RefObject | RefObject[]): A ref or an array of refs to the element(s) you want to detect outside clicks on.callback (function): A callback function that will be called when a click outside the detected elements occurs.events (string[], optional): An array of event names to listen for clicks. Defaults to ['mousedown', 'touchstart'].enableDetection (boolean, optional): A boolean to enable or disable click detection. Defaults to true.ignoreRefs (RefObject[], optional): An array of ref objects for elements that, when clicked, should not trigger the callback.refs are mounted when the hook is called.Feel free to contribute to the development of this hook by submitting issues or pull requests to the repository.
useClipboard is a React hook that provides a comprehensive interface for clipboard operations. It offers advanced features like automatic clipboard content reading, change detection, and polling capabilities, making it perfect for building sophisticated clipboard-aware applications.
$ claude mcp add custom-react-hooks \
-- python -m otcore.mcp_server <graph>