React Custom Hooks
This package contains predefined ready to use custom React hooks




Quick Start
To use the React Custom Hooks
in your React project, you can install it using npm or yarn:
npm install r-custom-hooks
yarn add r-custom-hooks
HOOKS & Usage Examples
useCookie
This hook is for managing cookies in your application.
import {useCookie} from "r-custom-hooks"
export default function CookieComponent() {
const [cookie, updateCookie, removeCookie] = useCookie("name", "John Doe")
return (
<>
<div>{cookie}</div>
<button onClick={() => updateCookie("Jane Doe")}>Change Name To Jane Doe</button>
<button onClick={removeCookie}>Delete Name</button>
</>
)
}
useDebounce
This is a hook for debouncing values in your application.
import React, { useState } from 'react';
import {useDebounce} from 'r-custom-hooks';
function MyComponent() {
const [inputValue, setInputValue] = useState('');
const debouncedValue = useDebounce(inputValue, 500);
const handleChange = (event) => {
setInputValue(event.target.value);
};
return (
<div>
<input
type="text"
value={inputValue}
onChange={handleChange}
placeholder="Type something..."
/>
<p>Debounced Value: {debouncedValue}</p>
</div>
);
}
export default MyComponent;
useEventListener
This is a hook for adding event listeners to elements or the window.
import React, { useRef } from 'react';
import {useEventListener} from 'r-custom-hooks';
import { EventType } from './types';
function MyComponent() {
const myElementRef = useRef(null);
const handleEvent = (event) => {
console.log('Event occurred:', event);
};
useEventListener(EventType.CLICK, handleEvent, myElementRef);
return (
<div>
<button ref={myElementRef}>Click me</button>
</div>
);
}
export default MyComponent;
useHistoricalState
This hook is for managing historical states with the ability to undo, redo, and navigate to specific states.
import React from 'react';
import {useHistoricalState} from 'r-custom-hooks';
function MyComponent() {
const [count, setCount, history] = useHistoricalState(0);
const handleIncrement = () => {
setCount(count + 1);
};
const handleDecrement = () => {
setCount(count - 1);
};
const handleUndo = () => {
history.previous();
};
const handleRedo = () => {
history.next();
};
const handleGoTo = (index) => {
history.go(index);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
<button onClick={handleUndo}>Undo</button>
<button onClick={handleRedo}>Redo</button>
<button onClick={() => handleGoTo(0)}>Go to Initial State</button>
</div>
);
}
export default MyComponent;
useOutsideClick
This is a hook for detecting clicks outside a specified element
import React, { useRef, useState } from 'react';
import {useOutsideClick} from 'r-custom-hooks';
function MyComponent() {
const containerRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);
const closeElement = () => {
setIsOpen(false);
};
useOutsideClick(containerRef, closeElement);
return (
<div>
<button onClick={() => setIsOpen(!isOpen)}>Toggle Element</button>
{isOpen && (
<div ref={containerRef} className="element-to-close">
</div>
)}
</div>
);
}
export default MyComponent;
useUnmount
This hook is for executing a callback when a component unmounts.
import React from 'react';
import {useUnmount} from 'r-custom-hooks';
function MyComponent() {
const cleanup = () => {
console.log('Component unmounted');
};
useUnmount(cleanup);
return (
<div>
<p>This is my component.</p>
</div>
);
}
export default MyComponent;