Hooks
Hooks
Hooks let you use different React features from your components.
You can either use the built-in Hooks or combine them to build your own.
State Hooks
Context Hooks
Context lets a component receive information from distant parents without passing
it as props.
For example, your app’s top-level component can pass the current UI theme to all
components below, no matter how deep.
useContext reads and subscribes to a context.
function Button() {
const theme = useContext(ThemeContext);
// ...
Ref Hooks
Refs let a component hold some information that isn’t used for rendering, like a
DOM node or a timeout ID.
Unlike with state, updating a ref does not re-render your component.
Refs are an “escape hatch” from the React paradigm.
They are useful when you need to work with non-React systems, such as the built-in
browser APIs.
useRef declares a ref. You can hold any value in it, but most often it’s used to hold a
DOM node.
useImperativeHandle lets you customize the ref exposed by your component. This is
rarely used.
function Form() {
const inputRef = useRef(null);
// ...
Effect Hooks
There are two rarely used variations of useEffect with differences in timing:
useLayoutEffect fires before the browser repaints the screen. You can measure
layout here.
useInsertionEffect fires before React makes changes to the DOM. Libraries can insert
dynamic CSS here.
Performance Hooks
Sometimes, you can’t skip re-rendering because the screen actually needs to update.
In that case, you can improve performance by separating blocking updates that must
be synchronous (like typing into an input) from non-blocking updates which don’t
need to block the user interface (like updating a chart).
useTransition lets you mark a state transition as non-blocking and allow other
updates to interrupt it.
useDeferredValue lets you defer updating a non-critical part of the UI and let other
parts update first.
You can also define your own custom Hooks as JavaScript functions.