HookVault
A comprehensive collection of TypeScript-first React hooks for building powerful and efficient applications
import { useState, useEffect } from 'react';
export function useLocalStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState<T>(() => {
if (typeof window === "undefined") {
return initialValue;
}
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);
return initialValue;
}
});
const setValue = (value: T | ((val: T) => T)) => {
try {
const valueToStore =
value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
if (typeof window !== "undefined") {
window.localStorage.setItem(key, JSON.stringify(valueToStore));
}
} catch (error) {
console.log(error);
}
};
return [storedValue, setValue] as const;
}
Features
Interactive Examples
Live code examples with Monaco Editor and Sandpack integration
Copy-Paste
Easily integrate hooks into your projects with simple copy and paste functionality
Well-Documented
Each hook comes with clear documentation and usage examples.
Trending Hooks
useDebounceCallback
Performance
A hook that debounces a callback function to limit its execution frequency
useDebounce
Performance
A hook for debouncing values to optimize performance
useFetch
Data Fetching
A custom hook for handling data fetching with loading and error states
useForm
Form
A hook for form state management and validation
useIntersectionObserver
Layout
A hook for tracking element visibility in the viewport
useLocalStorage
State
A hook for persisting state in localStorage with type safety