Composing Multiple Hooks in a Custom Hook
You'll learn how custom hooks can combine multiple built-in hooks to create powerful, reusable logic from simple building blocks.
The Core Idea
Custom hooks aren't limited to calling just one hook—they can call useState, useEffect, useCallback, and any other hooks you need, all within a single custom hook. This is where custom hooks truly shine: you can compose simple primitives into complex, domain-specific logic.
Think of it like cooking: individual ingredients (hooks) might be simple, but combining them creates a complete dish. A custom hook is your recipe that orchestrates multiple ingredients into something greater than the sum of its parts.
How It Works
When you write a custom hook, you can freely call multiple hooks inside it. Each hook follows the same rules (top-level, consistent order), but they all work together within your custom function:
function useFormField(initialValue) {
const [value, setValue] = useState(initialValue);
const [touched, setTouched] = useState(false);
const [error, setError] = useState(null);
const handleChange = (e) => {
setValue(e.target.value);
setError(null);
};
const handleBlur = () => {
setTouched(true);
};
return { value, touched, error, handleChange, handleBlur, setError };
}
This single hook composes three useState calls to manage related state. You could add useEffect for validation, useCallback for stable handlers, or any other hooks you need.
The Power of Composition
Each built-in hook does one thing well. Custom hooks let you combine them to match your specific problem domain—form management, data fetching with loading states, synchronized scroll positions, etc.
Key Takeaway: Custom hooks can call as many built-in hooks as needed, composing simple primitives into complex, reusable logic that perfectly fits your application's needs.