React 19 Hooks & Component Architecture
18 mins read
Free Lesson
Extract common stateful logic into custom hooks (useLocalStorage, useFetch, useWindowSize).
Custom Hooks Pattern
import { useState, useEffect } from 'react';
export function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
});
useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(storedValue));
}, [key, storedValue]);
return [storedValue, setStoredValue];
}