A collection of lightweight, production-ready, and SSR-safe custom React hooks designed to simplify state management, side effects, and DOM interactions in your web applications.
useAsync: Handles asynchronous functions (API calls, data fetching) with loading, success, and error states.useDebounce: Delays updating a value until a specified time has passed, perfect for live search inputs.useEventListener: Safely attaches and cleans up event listeners on elements likewindowordocument.useFetch: A robust data-fetching hook with built-inAbortControllersupport to prevent memory leaks.useLocalStorage: Syncs React state with browser'slocalStoragewith lazy initialization and error handling.useOnClickOutside: Detects clicks outside a specific DOM element (ideal for dropdowns, modals, and menus).usePrevious: Tracks and returns the previous value of a state or prop usinguseRef.useSessionStorage: Syncs React state with browser'ssessionStorage(clears when the tab closes).useToggle: Simple and clean boolean state toggler for UI controls.useWindowSize: Tracks current browser window dimensions with debounced performance optimization.
-
Clone the repository: git clone https://github.com/dev-sumitkumardudpuri/react-hooks-collection.git
-
Install dependencies: npm install
-
Example Usage:
import { useToggle } from "./src/hooks/useToggle";
function App() {
const [isOpen, toggleModal] = useToggle(false);
return (
<div>
<button onClick={toggleModal}>Toggle Modal</button>
{isOpen && <p>Modal is Open!</p>}
</div>
);
}- License: This project is open-source and available under the MIT License.