React hooks
Хуки React
These functions let React function components remember information between renders, read shared values, and stay in sync with systems outside React.
Also known as: hooks, хуки
What hooks give a component
Every time React renders a function component, it calls the function again to work out what the UI should show. A regular local variable starts over on each call, and changing it doesn't tell React to render again. With useState, React remembers a value between renders and gives you a setter to request an update. That's useful for things like the text in a search field or whether a menu is open.
Other hooks do different jobs. useContext reads a value from the nearest matching provider above the component. useRef can hold a DOM node or a timer ID; writing to ref.current doesn't trigger a render. useEffect is for keeping the component in sync with something outside React, such as a browser event subscription.
Why call order matters
React uses the order of ordinary hook calls to match them across renders. If a component calls two useState hooks and skips the first one on the next render, React can no longer match those calls as intended. Call hooks at the top level of a function component or a custom hook, before any conditional return. Don't call them inside loops, branches, or event handlers. React's use API is an exception: it can be called in conditions and loops, but still only from a component or hook.
A custom hook is a function named something like useWindowWidth that calls other hooks to package reusable logic. Two components that call the same custom hook reuse the implementation, not its local state. To share state, you need a common source, such as state held in a parent component and passed down as props.
Example: a disclosure button
import { useState } from 'react';
function Details() {
const [open, setOpen] = useState(false);
return (
<>
<button aria-expanded={open}
onClick={() => setOpen(previous => !previous)}>
Details
</button>
{open && <p>Orders ship on weekdays.</p>}
</>
);
}
The hook runs on every render, whether the details are visible or not. Only the paragraph is conditional. The click handler queues an update based on the previous state; it doesn't change the value of open in the handler that's already running.
Common mistakes and interview scenarios
If a value can be calculated from the current props or state, calculate it during render. For example, putting a filtered list in state and updating it in an effect creates a second copy of information the component already has. An effect makes sense for a subscription: it sets up the subscription after React commits the render, and its cleanup removes the old subscription before the effect runs again or when the component unmounts.
Interviewers often show an interval that keeps reading an old state value. Explain which render created the callback and which values it captured. If the callback only needs to increment state, a functional update like setCount(n => n + 1) avoids reading a captured count. If the subscription itself depends on a changing value, include that value in the effect's dependencies so React can replace the subscription when it changes.