Full answer
Before hooks, only class components could hold state or run code after rendering. Hooks, added in React 16.8, give function components the same abilities.
The most common ones are:
- useState keeps a value between renders and gives a setter that triggers a re-render.
- useEffect runs code after render to sync with something outside React, such as the document title, a timer or a network request.
- useContext reads a value from the nearest provider above the component.
- useRef holds a mutable value or a DOM node that does not cause a re-render when it changes.
- useMemo and useCallback cache a calculation or a function between renders.
- useReducer manages state through a reducer function.
There are two rules. First, call hooks only at the top level: never inside an if, a loop, or a nested function. Second, call hooks only from a function component or from another custom hook (a function whose name starts with use).
The reason is how React stores hooks. It does not know variable names. It keeps a list per component and matches each hook call to a slot by its position. If an if statement skips a hook on one render, every later hook shifts to the wrong slot. The eslint-plugin-react-hooks package catches these mistakes.
This is a wrong example, kept short on purpose:
import { useState } from 'react';
function Profile({ loggedIn }) {
if (loggedIn) {
const [name, setName] = useState('Asha'); // hook inside a condition
}
return null;
}In React 19 a context can be rendered directly as a provider, so <ThemeContext value={...}> replaces <ThemeContext.Provider value={...}>. The older form still works.
import { createContext, useContext, useEffect, useState } from 'react';
const ThemeContext = createContext(null);
function ThemeProvider({ children }) {
const [theme, setTheme] = useState('light');
useEffect(() => {
document.body.dataset.theme = theme;
}, [theme]);
return <ThemeContext value={{ theme, setTheme }}>{children}</ThemeContext>;
}
function ThemeButton() {
const { theme, setTheme } = useContext(ThemeContext);
return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Current theme: {theme}
</button>
);
}
export default function App() {
return (
<ThemeProvider>
<ThemeButton />
</ThemeProvider>
);
}useState owns the theme, useEffect copies it to the body element after each change, and useContext lets any descendant read it without props. Rendered result after one click: <button>Current theme: dark</button>
Key points
- Hooks let function components use state, effects, context, refs and other React features.
- Call hooks at the top level and only inside components or custom hooks.
- React matches hooks to stored slots by call order, which is why the order must be stable.
- useState, useEffect, useContext and useRef cover most everyday needs.
- In React 19 you can render a context object directly as its provider.
Common mistakes
- Calling a hook inside a condition, loop, event handler or after an early return.
- Calling hooks from ordinary helper functions whose names do not start with use, so the linter cannot check them.
- Expecting setState to change the variable immediately. The new value appears on the next render.
- Leaving out values from the useEffect dependency array and reading stale data.
Follow-ups
- Why does React need hooks to be called in the same order on every render?
- What is the difference between useEffect and useLayoutEffect?
- How would you write a custom hook, and why must its name start with use?
Tips
- Explain the call-order reason behind the rules. Reciting the rules without the reason sounds memorized.
- Name hooks by the problem they solve (remember a value, sync with the outside world) instead of listing them.
- Mention the lint plugin as the practical safety net.