Full answer
When two components need the same combination of useState, useEffect and other hooks, copying the code is error-prone. A custom hook turns that combination into one function you can call from both.
The rules are simple. The function name starts with use, so React and the linter know it follows the rules of hooks. Inside, it can call any hook. It returns whatever the caller needs, usually a value, or a value and a setter, or an object.
A key point: a custom hook does not share state between components. If two components call useLocalStorage('theme', 'light'), each gets its own useState. They only share the code. To share the data itself, lift state up or use context.
The example below reads a saved value on first render using a lazy initial state, so the read from storage happens once. It writes back in an effect whenever the value changes. The component using it knows nothing about storage.
Good custom hooks have one clear job, a small and stable return shape, and clean-up of anything they start. If the hook adds a listener or timer, it must remove it in the effect cleanup. For a hook that reads browser-only APIs, guard against server rendering, because window does not exist there. Parse errors from JSON.parse should also be handled in a production version.
Do not create a hook for a single useState. Create it when the logic has a name: useDebounce, useOnlineStatus, useMediaQuery. Test hooks by rendering them in a small component or with renderHook from React Testing Library.
import { useEffect, useState } from 'react';
function useLocalStorage(key, initialValue) {
const [value, setValue] = useState(() => {
const saved = window.localStorage.getItem(key);
return saved === null ? initialValue : JSON.parse(saved);
});
useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
}
export default function ThemeToggle() {
const [theme, setTheme] = useLocalStorage('theme', 'light');
return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Theme: {theme}
</button>
);
}The component stores its theme through the hook. After one click the button shows the dark theme and local storage holds the JSON value. Output of the run: stored theme="dark"
Key points
- A custom hook is a function named use... that calls other hooks.
- It shares logic. Every caller gets its own state.
- Return a small, stable shape and clean up listeners or timers in the effect.
- Guard browser-only APIs when the code can run on the server.
- Create a hook when the logic has a clear name and is used more than once.
Common mistakes
- Believing that two components calling the same hook share state.
- Naming a function without the use prefix, so the linter cannot check it.
- Calling window or localStorage during server rendering without a guard.
- Forgetting cleanup for listeners and timers inside the hook.
- Returning a new object every render when callers depend on a stable reference.
Follow-ups
- How would you write a useDebounce hook and test it?
- How would you let two components share the same stored value and stay in sync?
- When is a custom hook the wrong tool compared with a plain function?
Tips
- State early that custom hooks share logic, not state. Interviewers check this misconception.
- Explain the lazy initializer. It shows you know the callback runs only on the first render.
- Pick an example with a real effect and mention its cleanup needs.