Read the Motivation to learn why we’re introducing Hooks to React.
↑↑↑ Each section ends with a yellow box like this.
They link to detailed explanations.
📌 State Hook
This example renders a counter. When you click the button, it increments the value:
import React,{ useState }from'react'; functionExample(){ // Declare a new state variable, which we'll call "count"const[count, setCount]=useState(0); return( <div> <p>You clicked {count} times</p> <buttononClick={()=>setCount(count +1)}> Click me </button> </div> ); }
Here,
useState
is a
Hook
(we’ll talk about what this means in a moment). We call it inside a function component to add some local state to it. React will preserve this state between re-renders.
useState
returns a pair: the
current
state value and a function that lets you update it. You can call this function from an event handler or somewhere else. It’s similar to
this.setState
in a class, except it doesn’t merge the old and new state together. (We’ll show an example comparing
useState
to
this.state
in Using the State Hook.)
The only argument to
useState
is the initial state. In the example above, it is
0
because our counter starts from zero. Note that unlike
this.state
, the state here doesn’t have to be an object — although it can be if you want. The initial state argument is only used during the first render.
Declaring multiple state variables
You can use the State Hook more than once in a single component:
The array destructuring syntax lets us give different names to the state variables we declared by calling
useState
. These names aren’t a part of the
useState
API. Instead, React assumes that if you call
useState
many times, you do it in the same order during every render. We’ll come back to why this works and when this is useful later.
But what is a Hook?
Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don’t work inside classes — they let you use React without classes. (We don’t recommend rewriting your existing components overnight but you can start using Hooks in the new ones if you’d like.)
React provides a few built-in Hooks like
useState
. You can also create your own Hooks to reuse stateful behavior between different components. We’ll look at the built-in Hooks first.
Detailed Explanation
You can learn more about the State Hook on a dedicated page: Using the State Hook.
⚡️ Effect Hook
You’ve likely performed data fetching, subscriptions, or manually changing the DOM from React components before. We call these operations “side effects” (or “effects” for short) because they can affect other components and can’t be done during rendering.
The Effect Hook,
useEffect
, adds the ability to perform side effects from a function component. It serves the same purpose as
componentDidMount
,
componentDidUpdate
, and
componentWillUnmount
in React classes, but unified into a single API. (We’ll show examples comparing
useEffect
to these methods in Using the Effect Hook.)
For example, this component sets the document title after React updates the DOM:
// Similar to componentDidMount and componentDidUpdate:useEffect(()=>{// Update the document title using the browser API document.title =`You clicked ${count} times`;}); return( <div> <p>You clicked {count} times</p> <buttononClick={()=>setCount(count +1)}> Click me </button> </div> ); }
When you call
useEffect
, you’re telling React to run your “effect” function after flushing changes to the DOM. Effects are declared inside the component so they have access to its props and state. By default, React runs the effects after every render —
including
the first render. (We’ll talk more about how this compares to class lifecycles in Using the Effect Hook.)
Effects may also optionally specify how to “clean up” after them by returning a function. For example, this component uses an effect to subscribe to a friend’s online status, and cleans up by unsubscribing from it:
In this example, React would unsubscribe from our
ChatAPI
when the component unmounts, as well as before re-running the effect due to a subsequent render. (If you want, there’s a way to tell React to skip re-subscribing if the
props.friend.id
we passed to
ChatAPI
didn’t change.)
Just like with
useState
, you can use more than a single effect in a component:
Hooks let you organize side effects in a component by what pieces are related (such as adding and removing a subscription), rather than forcing a split based on lifecycle methods.
Detailed Explanation
You can learn more about
useEffect
on a dedicated page: Using the Effect Hook.
✌️ Rules of Hooks
Hooks are JavaScript functions, but they impose two additional rules:
Only call Hooks
at the top level
. Don’t call Hooks inside loops, conditions, or nested functions.
Only call Hooks
from React function components
. Don’t call Hooks from regular JavaScript functions. (There is just one other valid place to call Hooks — your own custom Hooks. We’ll learn about them in a moment.)
We provide a linter plugin to enforce these rules automatically. We understand these rules might seem limiting or confusing at first, but they are essential to making Hooks work well.
Detailed Explanation
You can learn more about these rules on a dedicated page: Rules of Hooks.
💡 Building Your Own Hooks
Sometimes, we want to reuse some stateful logic between components. Traditionally, there were two popular solutions to this problem: higher-order components and render props. Custom Hooks let you do this, but without adding more components to your tree.
Earlier on this page, we introduced a
FriendStatus
component that calls the
useState
and
useEffect
Hooks to subscribe to a friend’s online status. Let’s say we also want to reuse this subscription logic in another component.
First, we’ll extract this logic into a custom Hook called
useFriendStatus
:
The state of each component is completely independent. Hooks are a way to reuse
stateful logic
, not state itself. In fact, each
call
to a Hook has a completely isolated state — so you can even use the same custom Hook twice in one component.
Custom Hooks are more of a convention than a feature. If a function’s name starts with ”
use
” and it calls other Hooks, we say it is a custom Hook. The
useSomething
naming convention is how our linter plugin is able to find bugs in the code using Hooks.
You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven’t considered. We are excited to see what custom Hooks the React community will come up with.
Detailed Explanation
You can learn more about custom Hooks on a dedicated page: Building Your Own Hooks.
🔌 Other Hooks
There are a few less commonly used built-in Hooks that you might find useful. For example,
useContext
lets you subscribe to React context without introducing nesting:
You can learn more about all the built-in Hooks on a dedicated page: Hooks API Reference.
Next Steps
Phew, that was fast! If some things didn’t quite make sense or you’d like to learn more in detail, you can read the next pages, starting with the State Hook documentation.
You can also check out the Hooks API reference and the Hooks FAQ.
Finally, don’t miss the introduction page which explains
why
we’re adding Hooks and how we’ll start using them side by side with classes — without rewriting our apps.
Is this page useful?
Edit this page
Using the State Hook – React
Using the State Hook
Hooks
are a new addition in React 16.8. They let you use state and other React features without writing a class.
The state starts as
{ count: 0 }
, and we increment
state.count
when the user clicks a button by calling
this.setState()
. We’ll use snippets from this class throughout the page.
Note
You might be wondering why we’re using a counter here instead of a more realistic example. This is to help us focus on the API while we’re still making our first steps with Hooks.
Hooks and Function Components
As a reminder, function components in React look like this:
constExample=(props)=>{ // You can use Hooks here! return<div/>; }
or this:
functionExample(props){ // You can use Hooks here! return<div/>; }
You might have previously known these as “stateless components”. We’re now introducing the ability to use React state from these, so we prefer the name “function components”.
Hooks
don’t
work inside classes. But you can use them instead of writing classes.
What’s a Hook?
Our new example starts by importing the
useState
Hook from React:
What is a Hook?
A Hook is a special function that lets you “hook into” React features. For example,
useState
is a Hook that lets you add React state to function components. We’ll learn other Hooks later.
When would I use a Hook?
If you write a function component and realize you need to add some state to it, previously you had to convert it to a class. Now you can use a Hook inside the existing function component. We’re going to do that right now!
Note:
There are some special rules about where you can and can’t use Hooks within a component. We’ll learn them in Rules of Hooks.
Declaring a State Variable
In a class, we initialize the
count
state to
0
by setting
this.state
to
{ count: 0 }
in the constructor:
In a function component, we have no
this
, so we can’t assign or read
this.state
. Instead, we call the
useState
Hook directly inside our component:
import React,{ useState }from'react';
functionExample(){ // Declare a new state variable, which we'll call "count"const[count, setCount]=useState(0);
What does calling
useState
do?
It declares a “state variable”. Our variable is called
count
but we could call it anything else, like
banana
. This is a way to “preserve” some values between the function calls —
useState
is a new way to use the exact same capabilities that
this.state
provides in a class. Normally, variables “disappear” when the function exits but state variables are preserved by React.
What do we pass to
useState
as an argument?
The only argument to the
useState()
Hook is the initial state. Unlike with classes, the state doesn’t have to be an object. We can keep a number or a string if that’s all we need. In our example, we just want a number for how many times the user clicked, so pass
0
as initial state for our variable. (If we wanted to store two different values in state, we would call
useState()
twice.)
What does
useState
return?
It returns a pair of values: the current state and a function that updates it. This is why we write
const [count, setCount] = useState()
. This is similar to
this.state.count
and
this.setState
in a class, except you get them in a pair. If you’re not familiar with the syntax we used, we’ll come back to it at the bottom of this page.
Now that we know what the
useState
Hook does, our example should make more sense:
import React,{ useState }from'react';
functionExample(){ // Declare a new state variable, which we'll call "count"const[count, setCount]=useState(0);
We declare a state variable called
count
, and set it to
0
. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current
count
, we can call
setCount
.
Note
You might be wondering: why is
useState
not named
createState
instead?
“Create” wouldn’t be quite accurate because the state is only created the first time our component renders. During the next renders,
useState
gives us the current state. Otherwise it wouldn’t be “state” at all! There’s also a reason why Hook names
always
start with
use
. We’ll learn why later in the Rules of Hooks.
Reading State
When we want to display the current count in a class, we read
this.state.count
:
<p>You clicked {this.state.count} times</p>
In a function, we can use
count
directly:
<p>You clicked {count} times</p>
Updating State
In a class, we need to call
this.setState()
to update the
count
state:
<buttononClick={()=>this.setState({count:this.state.count +1})}> Click me </button>
In a function, we already have
setCount
and
count
as variables so we don’t need
this
:
<buttononClick={()=>setCount(count +1)}> Click me </button>
Recap
Let’s now
recap what we learned line by line
and check our understanding.
Line 1:
We import the
useState
Hook from React. It lets us keep local state in a function component.
Line 4:
Inside the
Example
component, we declare a new state variable by calling the
useState
Hook. It returns a pair of values, to which we give names. We’re calling our variable
count
because it holds the number of button clicks. We initialize it to zero by passing
0
as the only
useState
argument. The second returned item is itself a function. It lets us update the
count
so we’ll name it
setCount
.
Line 9:
When the user clicks, we call
setCount
with a new value. React will then re-render the
Example
component, passing the new
count
value to it.
This might seem like a lot to take in at first. Don’t rush it! If you’re lost in the explanation, look at the code above again and try to read it from top to bottom. We promise that once you try to “forget” how state works in classes, and look at this code with fresh eyes, it will make sense.
Tip: What Do Square Brackets Mean?
You might have noticed the square brackets when we declare a state variable:
const[count, setCount]=useState(0);
The names on the left aren’t a part of the React API. You can name your own state variables:
const[fruit, setFruit]=useState('banana');
This JavaScript syntax is called “array destructuring”. It means that we’re making two new variables
fruit
and
setFruit
, where
fruit
is set to the first value returned by
useState
, and
setFruit
is the second. It is equivalent to this code:
var fruitStateVariable =useState('banana');// Returns a pair var fruit = fruitStateVariable[0];// First item in a pair var setFruit = fruitStateVariable[1];// Second item in a pair
When we declare a state variable with
useState
, it returns a pair — an array with two items. The first item is the current value, and the second is a function that lets us update it. Using
[0]
and
[1]
to access them is a bit confusing because they have a specific meaning. This is why we use array destructuring instead.
Note
You might be curious how React knows which component
useState
corresponds to since we’re not passing anything like
this
back to React. We’ll answer this question and many others in the FAQ section.
Tip: Using Multiple State Variables
Declaring state variables as a pair of
[something, setSomething]
is also handy because it lets us give
different
names to different state variables if we want to use more than one:
In the above component, we have
age
,
fruit
, and
todos
as local variables, and we can update them individually:
functionhandleOrangeClick(){ // Similar to this.setState({ fruit: 'orange' }) setFruit('orange'); }
You
don’t have to
use many state variables. State variables can hold objects and arrays just fine, so you can still group related data together. However, unlike
this.setState
in a class, updating a state variable always
replaces
it instead of merging it.
We provide more recommendations on splitting independent state variables in the FAQ.
Next Steps
On this page we’ve learned about one of the Hooks provided by React, called
useState
. We’re also sometimes going to refer to it as the “State Hook”. It lets us add local state to React function components — which we did for the first time ever!
We also learned a little bit more about what Hooks are. Hooks are functions that let you “hook into” React features from function components. Their names always start with
use
, and there are more Hooks we haven’t seen yet.
Now let’s continue by learning the next Hook:
useEffect
.
It lets you perform side effects in components, and is similar to lifecycle methods in classes.
// Similar to componentDidMount and componentDidUpdate:useEffect(()=>{// Update the document title using the browser API document.title =`You clicked ${count} times`;}); return( <div> <p>You clicked {count} times</p> <buttononClick={()=>setCount(count +1)}> Click me </button> </div> ); }
Data fetching, setting up a subscription, and manually changing the DOM in React components are all examples of side effects. Whether or not you’re used to calling these operations “side effects” (or just “effects”), you’ve likely performed them in your components before.
Tip
If you’re familiar with React class lifecycle methods, you can think of
useEffect
Hook as
componentDidMount
,
componentDidUpdate
, and
componentWillUnmount
combined.
There are two common kinds of side effects in React components: those that don’t require cleanup, and those that do. Let’s look at this distinction in more detail.
Effects Without Cleanup
Sometimes, we want to
run some additional code after React has updated the DOM.
Network requests, manual DOM mutations, and logging are common examples of effects that don’t require a cleanup. We say that because we can run them and immediately forget about them. Let’s compare how classes and Hooks let us express such side effects.
Example Using Classes
In React class components, the
render
method itself shouldn’t cause side effects. It would be too early — we typically want to perform our effects
after
React has updated the DOM.
This is why in React classes, we put side effects into
componentDidMount
and
componentDidUpdate
. Coming back to our example, here is a React counter class component that updates the document title right after React makes changes to the DOM:
Note how
we have to duplicate the code between these two lifecycle methods in class.
This is because in many cases we want to perform the same side effect regardless of whether the component just mounted, or if it has been updated. Conceptually, we want it to happen after every render — but React class components don’t have a method like this. We could extract a separate method but we would still have to call it in two places.
Now let’s see how we can do the same with the
useEffect
Hook.
Example Using Hooks
We’ve already seen this example at the top of this page, but let’s take a closer look at it:
What does
useEffect
do?
By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates. In this effect, we set the document title, but we could also perform data fetching or call some other imperative API.
Why is
useEffect
called inside a component?
Placing
useEffect
inside the component lets us access the
count
state variable (or any props) right from the effect. We don’t need a special API to read it — it’s already in the function scope. Hooks embrace JavaScript closures and avoid introducing React-specific APIs where JavaScript already provides a solution.
Does
useEffect
run after every render?
Yes! By default, it runs both after the first render
and
after every update. (We will later talk about how to customize this.) Instead of thinking in terms of “mounting” and “updating”, you might find it easier to think that effects happen “after render”. React guarantees the DOM has been updated by the time it runs the effects.
Detailed Explanation
Now that we know more about effects, these lines should make sense:
We declare the
count
state variable, and then we tell React we need to use an effect. We pass a function to the
useEffect
Hook. This function we pass
is
our effect. Inside our effect, we set the document title using the
document.title
browser API. We can read the latest
count
inside the effect because it’s in the scope of our function. When React renders our component, it will remember the effect we used, and then run our effect after updating the DOM. This happens for every render, including the first one.
Experienced JavaScript developers might notice that the function passed to
useEffect
is going to be different on every render. This is intentional. In fact, this is what lets us read the
count
value from inside the effect without worrying about it getting stale. Every time we re-render, we schedule a
different
effect, replacing the previous one. In a way, this makes the effects behave more like a part of the render result — each effect “belongs” to a particular render. We will see more clearly why this is useful later on this page.
Tip
Unlike
componentDidMount
or
componentDidUpdate
, effects scheduled with
useEffect
don’t block the browser from updating the screen. This makes your app feel more responsive. The majority of effects don’t need to happen synchronously. In the uncommon cases where they do (such as measuring the layout), there is a separate
useLayoutEffect
Hook with an API identical to
useEffect
.
Effects with Cleanup
Earlier, we looked at how to express side effects that don’t require any cleanup. However, some effects do. For example,
we might want to set up a subscription
to some external data source. In that case, it is important to clean up so that we don’t introduce a memory leak! Let’s compare how we can do it with classes and with Hooks.
Example Using Classes
In a React class, you would typically set up a subscription in
componentDidMount
, and clean it up in
componentWillUnmount
. For example, let’s say we have a
ChatAPI
module that lets us subscribe to a friend’s online status. Here’s how we might subscribe and display that status using a class:
Notice how
componentDidMount
and
componentWillUnmount
need to mirror each other. Lifecycle methods force us to split this logic even though conceptually code in both of them is related to the same effect.
Note
Eagle-eyed readers may notice that this example also needs a
componentDidUpdate
method to be fully correct. We’ll ignore this for now but will come back to it in a later section of this page.
Example Using Hooks
Let’s see how we could write this component with Hooks.
You might be thinking that we’d need a separate effect to perform the cleanup. But code for adding and removing a subscription is so tightly related that
useEffect
is designed to keep it together. If your effect returns a function, React will run it when it is time to clean up:
useEffect(()=>{functionhandleStatusChange(status){setIsOnline(status.isOnline);} ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);// Specify how to clean up after this effect:returnfunctioncleanup(){ ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);};}); if(isOnline ===null){ return'Loading...'; } return isOnline ?'Online':'Offline'; }
Why did we return a function from our effect?
This is the optional cleanup mechanism for effects. Every effect may return a function that cleans up after it. This lets us keep the logic for adding and removing subscriptions close to each other. They’re part of the same effect!
When exactly does React clean up an effect?
React performs the cleanup when the component unmounts. However, as we learned earlier, effects run for every render and not just once. This is why React
also
cleans up effects from the previous render before running the effects next time. We’ll discuss why this helps avoid bugs and how to opt out of this behavior in case it creates performance issues later below.
Note
We don’t have to return a named function from the effect. We called it
cleanup
here to clarify its purpose, but you could return an arrow function or call it something different.
Recap
We’ve learned that
useEffect
lets us express different kinds of side effects after a component renders. Some effects might require cleanup so they return a function:
The Effect Hook unifies both use cases with a single API.
If you feel like you have a decent grasp on how the Effect Hook works, or if you feel overwhelmed, you can jump to the next page about Rules of Hooks now.
Tips for Using Effects
We’ll continue this page with an in-depth look at some aspects of
useEffect
that experienced React users will likely be curious about. Don’t feel obligated to dig into them now. You can always come back to this page to learn more details about the Effect Hook.
Tip: Use Multiple Effects to Separate Concerns
One of the problems we outlined in the Motivation for Hooks is that class lifecycle methods often contain unrelated logic, but related logic gets broken up into several methods. Here is a component that combines the counter and the friend status indicator logic from the previous examples:
Note how the logic that sets
document.title
is split between
componentDidMount
and
componentDidUpdate
. The subscription logic is also spread between
componentDidMount
and
componentWillUnmount
. And
componentDidMount
contains code for both tasks.
So, how can Hooks solve this problem? Just like you can use the
State
Hook more than once, you can also use several effects. This lets us separate unrelated logic into different effects:
Hooks let us split the code based on what it is doing
rather than a lifecycle method name. React will apply
every
effect used by the component, in the order they were specified.
Explanation: Why Effects Run on Each Update
If you’re used to classes, you might be wondering why the effect cleanup phase happens after every re-render, and not just once during unmounting. Let’s look at a practical example to see why this design helps us create components with fewer bugs.
Earlier on this page, we introduced an example
FriendStatus
component that displays whether a friend is online or not. Our class reads
friend.id
from
this.props
, subscribes to the friend status after the component mounts, and unsubscribes during unmounting:
But what happens if the
friend
prop changes
while the component is on the screen? Our component would continue displaying the online status of a different friend. This is a bug. We would also cause a memory leak or crash when unmounting since the unsubscribe call would use the wrong friend ID.
In a class component, we would need to add
componentDidUpdate
to handle this case:
componentDidUpdate(prevProps){// Unsubscribe from the previous friend.id ChatAPI.unsubscribeFromFriendStatus( prevProps.friend.id,this.handleStatusChange);// Subscribe to the next friend.id ChatAPI.subscribeToFriendStatus(this.props.friend.id,this.handleStatusChange);} componentWillUnmount(){ ChatAPI.unsubscribeFromFriendStatus( this.props.friend.id, this.handleStatusChange ); }
Forgetting to handle
componentDidUpdate
properly is a common source of bugs in React applications.
Now consider the version of this component that uses Hooks:
It doesn’t suffer from this bug. (But we also didn’t make any changes to it.)
There is no special code for handling updates because
useEffect
handles them
by default
. It cleans up the previous effects before applying the next effects. To illustrate this, here is a sequence of subscribe and unsubscribe calls that this component could produce over time:
// Mount with { friend: { id: 100 } } props ChatAPI.subscribeToFriendStatus(100, handleStatusChange);// Run first effect
// Update with { friend: { id: 200 } } props ChatAPI.unsubscribeFromFriendStatus(100, handleStatusChange);// Clean up previous effect ChatAPI.subscribeToFriendStatus(200, handleStatusChange);// Run next effect
// Update with { friend: { id: 300 } } props ChatAPI.unsubscribeFromFriendStatus(200, handleStatusChange);// Clean up previous effect ChatAPI.subscribeToFriendStatus(300, handleStatusChange);// Run next effect
// Unmount ChatAPI.unsubscribeFromFriendStatus(300, handleStatusChange);// Clean up last effect
This behavior ensures consistency by default and prevents bugs that are common in class components due to missing update logic.
Tip: Optimizing Performance by Skipping Effects
In some cases, cleaning up or applying the effect after every render might create a performance problem. In class components, we can solve this by writing an extra comparison with
prevProps
or
prevState
inside
componentDidUpdate
:
This requirement is common enough that it is built into the
useEffect
Hook API. You can tell React to
skip
applying an effect if certain values haven’t changed between re-renders. To do so, pass an array as an optional second argument to
useEffect
:
useEffect(()=>{ document.title =`You clicked ${count} times`; },[count]);// Only re-run the effect if count changes
In the example above, we pass
[count]
as the second argument. What does this mean? If the
count
is
5
, and then our component re-renders with
count
still equal to
5
, React will compare
[5]
from the previous render and
[5]
from the next render. Because all items in the array are the same (
5 === 5
), React would skip the effect. That’s our optimization.
When we render with
count
updated to
6
, React will compare the items in the
[5]
array from the previous render to items in the
[6]
array from the next render. This time, React will re-apply the effect because
5 !== 6
. If there are multiple items in the array, React will re-run the effect even if just one of them is different.
This also works for effects that have a cleanup phase:
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange); return()=>{ ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange); }; },[props.friend.id]);// Only re-subscribe if props.friend.id changes
In the future, the second argument might get added automatically by a build-time transformation.
Note
If you use this optimization, make sure the array includes
all values from the component scope (such as props and state) that change over time and that are used by the effect
. Otherwise, your code will reference stale values from previous renders. Learn more about how to deal with functions and what to do when the array changes too often.
If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array (
[]
) as a second argument. This tells React that your effect doesn’t depend on
any
values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.
If you pass an empty array (
[]
), the props and state inside the effect will always have their initial values. While passing
[]
as the second argument is closer to the familiar
componentDidMount
and
componentWillUnmount
mental model, there are usually better solutions to avoid re-running effects too often. Also, don’t forget that React defers running
useEffect
until after the browser has painted, so doing extra work is less of a problem.
We recommend using the
exhaustive-deps
rule as part of our
eslint-plugin-react-hooks
package. It warns when dependencies are specified incorrectly and suggests a fix.
Next Steps
Congratulations! This was a long page, but hopefully by the end most of your questions about effects were answered. You’ve learned both the State Hook and the Effect Hook, and there is a
lot
you can do with both of them combined. They cover most of the use cases for classes — and where they don’t, you might find the additional Hooks helpful.
We’re also starting to see how Hooks solve problems outlined in Motivation. We’ve seen how effect cleanup avoids duplication in
componentDidUpdate
and
componentWillUnmount
, brings related code closer together, and helps us avoid bugs. We’ve also seen how we can separate effects by their purpose, which is something we couldn’t do in classes at all.
At this point you might be questioning how Hooks work. How can React know which
useState
call corresponds to which state variable between re-renders? How does React “match up” previous and next effects on every update?
On the next page we will learn about the Rules of Hooks — they’re essential to making Hooks work.
Don’t call Hooks inside loops, conditions, or nested functions.
Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That’s what allows React to correctly preserve the state of Hooks between multiple
useState
and
useEffect
calls. (If you’re curious, we’ll explain this in depth below.)
Only Call Hooks from React Functions
Don’t call Hooks from regular JavaScript functions.
Instead, you can:
✅ Call Hooks from React function components.
✅ Call Hooks from custom Hooks (we’ll learn about them on the next page).
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
ESLint Plugin
We released an ESLint plugin called
eslint-plugin-react-hooks
that enforces these two rules. You can add this plugin to your project if you’d like to try it:
This plugin is included by default in Create React App.
You can skip to the next page explaining how to write your own Hooks now.
On this page, we’ll continue by explaining the reasoning behind these rules.
Explanation
As we learned earlier, we can use multiple State or Effect Hooks in a single component:
functionForm(){ // 1. Use the name state variable const[name, setName]=useState('Mary');
// 2. Use an effect for persisting the form useEffect(functionpersistForm(){ localStorage.setItem('formData', name); });
// 3. Use the surname state variable const[surname, setSurname]=useState('Poppins');
// 4. Use an effect for updating the title useEffect(functionupdateTitle(){ document.title = name +' '+ surname; });
// ... }
So how does React know which state corresponds to which
useState
call? The answer is that
React relies on the order in which Hooks are called
. Our example works because the order of the Hook calls is the same on every render:
// ------------ // First render // ------------ useState('Mary')// 1. Initialize the name state variable with 'Mary' useEffect(persistForm)// 2. Add an effect for persisting the form useState('Poppins')// 3. Initialize the surname state variable with 'Poppins' useEffect(updateTitle)// 4. Add an effect for updating the title
// ------------- // Second render // ------------- useState('Mary')// 1. Read the name state variable (argument is ignored) useEffect(persistForm)// 2. Replace the effect for persisting the form useState('Poppins')// 3. Read the surname state variable (argument is ignored) useEffect(updateTitle)// 4. Replace the effect for updating the title
// ...
As long as the order of the Hook calls is the same between renders, React can associate some local state with each of them. But what happens if we put a Hook call (for example, the
persistForm
effect) inside a condition?
// 🔴 We're breaking the first rule by using a Hook in a condition if(name !==''){ useEffect(functionpersistForm(){ localStorage.setItem('formData', name); }); }
The
name !== ''
condition is
true
on the first render, so we run this Hook. However, on the next render the user might clear the form, making the condition
false
. Now that we skip this Hook during rendering, the order of the Hook calls becomes different:
useState('Mary')// 1. Read the name state variable (argument is ignored) // useEffect(persistForm) // 🔴 This Hook was skipped! useState('Poppins')// 🔴 2 (but was 3). Fail to read the surname state variable useEffect(updateTitle)// 🔴 3 (but was 4). Fail to replace the effect
React wouldn’t know what to return for the second
useState
Hook call. React expected that the second Hook call in this component corresponds to the
persistForm
effect, just like during the previous render, but it doesn’t anymore. From that point, every next Hook call after the one we skipped would also shift by one, leading to bugs.
This is why Hooks must be called on the top level of our components.
If we want to run an effect conditionally, we can put that condition
inside
our Hook:
useEffect(functionpersistForm(){ // 👍 We're not breaking the first rule anymore if(name !==''){ localStorage.setItem('formData', name); } });
Note that you don’t need to worry about this problem if you use the provided lint rule.
But now you also know
why
Hooks work this way, and which issues the rule is preventing.
Next Steps
Finally, we’re ready to learn about writing your own Hooks! Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
Now let’s say that our chat application also has a contact list, and we want to render names of online users with a green color. We could copy and paste similar logic above into our
FriendListItem
component but it wouldn’t be ideal:
Instead, we’d like to share this logic between
FriendStatus
and
FriendListItem
.
Traditionally in React, we’ve had two popular ways to share stateful logic between components: render props and higher-order components. We will now look at how Hooks solve many of the same problems without forcing you to add more components to the tree.
Extracting a Custom Hook
When we want to share logic between two JavaScript functions, we extract it to a third function. Both components and Hooks are functions, so this works for them too!
A custom Hook is a JavaScript function whose name starts with ”
use
” and that may call other Hooks.
For example,
useFriendStatus
below is our first custom Hook:
There’s nothing new inside of it — the logic is copied from the components above. Just like in a component, make sure to only call other Hooks unconditionally at the top level of your custom Hook.
Unlike a React component, a custom Hook doesn’t need to have a specific signature. We can decide what it takes as arguments, and what, if anything, it should return. In other words, it’s just like a normal function. Its name should always start with
use
so that you can tell at a glance that the rules of Hooks apply to it.
The purpose of our
useFriendStatus
Hook is to subscribe us to a friend’s status. This is why it takes
friendID
as an argument, and returns whether this friend is online:
In the beginning, our stated goal was to remove the duplicated logic from the
FriendStatus
and
FriendListItem
components. Both of them want to know whether a friend is online.
Now that we’ve extracted this logic to a
useFriendStatus
hook, we can
just use it:
Is this code equivalent to the original examples?
Yes, it works in exactly the same way. If you look closely, you’ll notice we didn’t make any changes to the behavior. All we did was to extract some common code between two functions into a separate function.
Custom Hooks are a convention that naturally follows from the design of Hooks, rather than a React feature.
Do I have to name my custom Hooks starting with “
use
”?
Please do. This convention is very important. Without it, we wouldn’t be able to automatically check for violations of rules of Hooks because we couldn’t tell if a certain function contains calls to Hooks inside of it.
Do two components using the same Hook share state?
No. Custom Hooks are a mechanism to reuse
stateful logic
(such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.
How does a custom Hook get isolated state?
Each
call
to a Hook gets isolated state. Because we call
useFriendStatus
directly, from React’s point of view our component just calls
useState
and
useEffect
. And as we learned earlier, we can call
useState
and
useEffect
many times in one component, and they will be completely independent.
Tip: Pass Information Between Hooks
Since Hooks are functions, we can pass information between them.
To illustrate this, we’ll use another component from our hypothetical chat example. This is a chat message recipient picker that displays whether the currently selected friend is online:
We keep the currently chosen friend ID in the
recipientID
state variable, and update it if the user chooses a different friend in the
<select>
picker.
Because the
useState
Hook call gives us the latest value of the
recipientID
state variable, we can pass it to our custom
useFriendStatus
Hook as an argument:
This lets us know whether the
currently selected
friend is online. If we pick a different friend and update the
recipientID
state variable, our
useFriendStatus
Hook will unsubscribe from the previously selected friend, and subscribe to the status of the newly selected one.
useYourImagination()
Custom Hooks offer the flexibility of sharing logic that wasn’t possible in React components before. You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven’t considered. What’s more, you can build Hooks that are just as easy to use as React’s built-in features.
Try to resist adding abstraction too early. Now that function components can do more, it’s likely that the average function component in your codebase will become longer. This is normal — don’t feel like you
have to
immediately split it into Hooks. But we also encourage you to start spotting cases where a custom Hook could hide complex logic behind a simple interface, or help untangle a messy component.
For example, maybe you have a complex component that contains a lot of local state that is managed in an ad-hoc way.
useState
doesn’t make centralizing the update logic any easier so you might prefer to write it as a Redux reducer:
Reducers are very convenient to test in isolation, and scale to express complex update logic. You can further break them apart into smaller reducers if necessary. However, you might also enjoy the benefits of using React local state, or might not want to install another library.
So what if we could write a
useReducer
Hook that lets us manage the
local
state of our component with a reducer? A simplified version of it might look like this:
Now we could use it in our component, and let the reducer drive its state management:
functionTodos(){ const[todos, dispatch]=useReducer(todosReducer,[]); functionhandleAddClick(text){ dispatch({type:'add', text }); }
// ... }
The need to manage local state with a reducer in a complex component is common enough that we’ve built the
useReducer
Hook right into React. You’ll find it together with other built-in Hooks in the Hooks API reference.
Returns a stateful value, and a function to update it.
During the initial render, the returned state (
state
) is the same as the value passed as the first argument (
initialState
).
The
setState
function is used to update the state. It accepts a new state value and enqueues a re-render of the component.
setState(newState);
During subsequent re-renders, the first value returned by
useState
will always be the most recent state after applying updates.
Note
React guarantees that
setState
function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the
useEffect
or
useCallback
dependency list.
Functional updates
If the new state is computed using the previous state, you can pass a function to
setState
. The function will receive the previous value, and return an updated value. Here’s an example of a counter component that uses both forms of
setState
:
The ”+” and ”-” buttons use the functional form, because the updated value is based on the previous value. But the “Reset” button uses the normal form, because it always sets the count back to the initial value.
If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely.
Note
Unlike the
setState
method found in class components,
useState
does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax:
const[state, setState]=useState({}); setState(prevState=>{ // Object.assign would also work return{...prevState,...updatedValues}; });
Another option is
useReducer
, which is more suited for managing state objects that contain multiple sub-values.
Lazy initial state
The
initialState
argument is the state used during the initial render. In subsequent renders, it is disregarded. If the initial state is the result of an expensive computation, you may provide a function instead, which will be executed only on the initial render:
If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the
Object.is
comparison algorithm.)
Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with
useMemo
.
Batching of state updates
React may group several state updates into a single re-render to improve performance. Normally, this improves performance and shouldn’t affect your application’s behavior.
Before React 18, only updates inside React event handlers were batched. Starting with React 18, batching is enabled for all updates by default. Note that React makes sure that updates from several
different
user-initiated events — for example, clicking a button twice — are always processed separately and do not get batched. This prevents logical mistakes.
In the rare case that you need to force the DOM update to be applied synchronously, you may wrap it in
flushSync
. However, this can hurt performance so do this only where needed.
useEffect
useEffect(didUpdate);
Accepts a function that contains imperative, possibly effectful code.
Mutations, subscriptions, timers, logging, and other side effects are not allowed inside the main body of a function component (referred to as React’s
render phase
). Doing so will lead to confusing bugs and inconsistencies in the UI.
Instead, use
useEffect
. The function passed to
useEffect
will run after the render is committed to the screen. Think of effects as an escape hatch from React’s purely functional world into the imperative world.
By default, effects run after every completed render, but you can choose to fire them only when certain values have changed.
Cleaning up an effect
Often, effects create resources that need to be cleaned up before the component leaves the screen, such as a subscription or timer ID. To do this, the function passed to
useEffect
may return a clean-up function. For example, to create a subscription:
useEffect(()=>{ const subscription = props.source.subscribe(); return()=>{ // Clean up the subscription subscription.unsubscribe(); }; });
The clean-up function runs before the component is removed from the UI to prevent memory leaks. Additionally, if a component renders multiple times (as they typically do), the
previous effect is cleaned up before executing the next effect
. In our example, this means a new subscription is created on every update. To avoid firing an effect on every update, refer to the next section.
Timing of effects
Unlike
componentDidMount
and
componentDidUpdate
, the function passed to
useEffect
fires
after
layout and paint, during a deferred event. This makes it suitable for the many common side effects, like setting up subscriptions and event handlers, because most types of work shouldn’t block the browser from updating the screen.
However, not all effects can be deferred. For example, a DOM mutation that is visible to the user must fire synchronously before the next paint so that the user does not perceive a visual inconsistency. (The distinction is conceptually similar to passive versus active event listeners.) For these types of effects, React provides one additional Hook called
useLayoutEffect
. It has the same signature as
useEffect
, and only differs in when it is fired.
Additionally, starting in React 18, the function passed to
useEffect
will fire synchronously
before
layout and paint when it’s the result of a discrete user input such as a click, or when it’s the result of an update wrapped in
flushSync
. This behavior allows the result of the effect to be observed by the event system, or by the caller of
flushSync
.
Note
This only affects the timing of when the function passed to
useEffect
is called - updates scheduled inside these effects are still deferred. This is different than
useLayoutEffect
, which fires the function and processes the updates inside of it immediately.
Even in cases where
useEffect
is deferred until after the browser has painted, it’s guaranteed to fire before any new renders. React will always flush a previous render’s effects before starting a new update.
Conditionally firing an effect
The default behavior for effects is to fire the effect after every completed render. That way an effect is always recreated if one of its dependencies changes.
However, this may be overkill in some cases, like the subscription example from the previous section. We don’t need to create a new subscription on every update, only if the
source
prop has changed.
To implement this, pass a second argument to
useEffect
that is the array of values that the effect depends on. Our updated example now looks like this:
Now the subscription will only be recreated when
props.source
changes.
Note
If you use this optimization, make sure the array includes
all values from the component scope (such as props and state) that change over time and that are used by the effect
. Otherwise, your code will reference stale values from previous renders. Learn more about how to deal with functions and what to do when the array values change too often.
If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array (
[]
) as a second argument. This tells React that your effect doesn’t depend on
any
values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.
If you pass an empty array (
[]
), the props and state inside the effect will always have their initial values. While passing
[]
as the second argument is closer to the familiar
componentDidMount
and
componentWillUnmount
mental model, there are usually better solutions to avoid re-running effects too often. Also, don’t forget that React defers running
useEffect
until after the browser has painted, so doing extra work is less of a problem.
We recommend using the
exhaustive-deps
rule as part of our
eslint-plugin-react-hooks
package. It warns when dependencies are specified incorrectly and suggests a fix.
The array of dependencies is not passed as arguments to the effect function. Conceptually, though, that’s what they represent: every value referenced inside the effect function should also appear in the dependencies array. In the future, a sufficiently advanced compiler could create this array automatically.
useContext
const value =useContext(MyContext);
Accepts a context object (the value returned from
React.createContext
) and returns the current context value for that context. The current context value is determined by the
value
prop of the nearest
<MyContext.Provider>
above the calling component in the tree.
When the nearest
<MyContext.Provider>
above the component updates, this Hook will trigger a rerender with the latest context
value
passed to that
MyContext
provider. Even if an ancestor uses
React.memo
or
shouldComponentUpdate
, a rerender will still happen starting at the component itself using
useContext
.
Don’t forget that the argument to
useContext
must be the
context object itself
:
Correct:
useContext(MyContext)
Incorrect:
useContext(MyContext.Consumer)
Incorrect:
useContext(MyContext.Provider)
A component calling
useContext
will always re-render when the context value changes. If re-rendering the component is expensive, you can optimize it by using memoization.
Tip
If you’re familiar with the context API before Hooks,
useContext(MyContext)
is equivalent to
static contextType = MyContext
in a class, or to
<MyContext.Consumer>
.
useContext(MyContext)
only lets you
read
the context and subscribe to its changes. You still need a
<MyContext.Provider>
above in the tree to
provide
the value for this context.
functionThemedButton(){ const theme =useContext(ThemeContext);return(<buttonstyle={{background: theme.background,color: theme.foreground }}> I am styled by theme context!</button>); }
This example is modified for hooks from a previous example in the Context Advanced Guide, where you can find more information about when and how to use Context.
Additional Hooks
The following Hooks are either variants of the basic ones from the previous section, or only needed for specific edge cases. Don’t stress about learning them up front.
An alternative to
useState
. Accepts a reducer of type
(state, action) => newState
, and returns the current state paired with a
dispatch
method. (If you’re familiar with Redux, you already know how this works.)
useReducer
is usually preferable to
useState
when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one.
useReducer
also lets you optimize performance for components that trigger deep updates because you can pass
dispatch
down instead of callbacks.
Here’s the counter example from the
useState
section, rewritten to use a reducer:
React guarantees that
dispatch
function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the
useEffect
or
useCallback
dependency list.
Specifying the initial state
There are two different ways to initialize
useReducer
state. You may choose either one depending on the use case. The simplest way is to pass the initial state as a second argument:
React doesn’t use the
state = initialState
argument convention popularized by Redux. The initial value sometimes needs to depend on props and so is specified from the Hook call instead. If you feel strongly about this, you can call
useReducer(reducer, undefined, reducer)
to emulate the Redux behavior, but it’s not encouraged.
Lazy initialization
You can also create the initial state lazily. To do this, you can pass an
init
function as the third argument. The initial state will be set to
init(initialArg)
.
It lets you extract the logic for calculating the initial state outside the reducer. This is also handy for resetting the state later in response to an action:
If you return the same value from a Reducer Hook as the current state, React will bail out without rendering the children or firing effects. (React uses the
Object.is
comparison algorithm.)
Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with
useMemo
.
Pass an inline callback and an array of dependencies.
useCallback
will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g.
shouldComponentUpdate
).
useCallback(fn, deps)
is equivalent to
useMemo(() => fn, deps)
.
Note
The array of dependencies is not passed as arguments to the callback. Conceptually, though, that’s what they represent: every value referenced inside the callback should also appear in the dependencies array. In the future, a sufficiently advanced compiler could create this array automatically.
We recommend using the
exhaustive-deps
rule as part of our
eslint-plugin-react-hooks
package. It warns when dependencies are specified incorrectly and suggests a fix.
Pass a “create” function and an array of dependencies.
useMemo
will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.
Remember that the function passed to
useMemo
runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in
useEffect
, not
useMemo
.
If no array is provided, a new value will be computed on every render.
You may rely on
useMemo
as a performance optimization, not as a semantic guarantee.
In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without
useMemo
— and then add it to optimize performance.
Note
The array of dependencies is not passed as arguments to the function. Conceptually, though, that’s what they represent: every value referenced inside the function should also appear in the dependencies array. In the future, a sufficiently advanced compiler could create this array automatically.
We recommend using the
exhaustive-deps
rule as part of our
eslint-plugin-react-hooks
package. It warns when dependencies are specified incorrectly and suggests a fix.
useRef
const refContainer =useRef(initialValue);
useRef
returns a mutable ref object whose
.current
property is initialized to the passed argument (
initialValue
). The returned object will persist for the full lifetime of the component.
A common use case is to access a child imperatively:
functionTextInputWithFocusButton(){ const inputEl =useRef(null); constonButtonClick=()=>{ // `current` points to the mounted text input element inputEl.current.focus(); }; return( <> <inputref={inputEl}type="text"/> <buttononClick={onButtonClick}>Focus the input</button> </> ); }
Essentially,
useRef
is like a “box” that can hold a mutable value in its
.current
property.
You might be familiar with refs primarily as a way to access the DOM. If you pass a ref object to React with
<div ref={myRef} />
, React will set its
.current
property to the corresponding DOM node whenever that node changes.
However,
useRef()
is useful for more than the
ref
attribute. It’s handy for keeping any mutable value around similar to how you’d use instance fields in classes.
This works because
useRef()
creates a plain JavaScript object. The only difference between
useRef()
and creating a
{current: ...}
object yourself is that
useRef
will give you the same ref object on every render.
Keep in mind that
useRef
doesn’t
notify you when its content changes. Mutating the
.current
property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a callback ref instead.
useImperativeHandle
useImperativeHandle(ref, createHandle,[deps])
useImperativeHandle
customizes the instance value that is exposed to parent components when using
ref
. As always, imperative code using refs should be avoided in most cases.
useImperativeHandle
should be used with
forwardRef
:
In this example, a parent component that renders
<FancyInput ref={inputRef} />
would be able to call
inputRef.current.focus()
.
useLayoutEffect
The signature is identical to
useEffect
, but it fires synchronously after all DOM mutations. Use this to read layout from the DOM and synchronously re-render. Updates scheduled inside
useLayoutEffect
will be flushed synchronously, before the browser has a chance to paint.
Prefer the standard
useEffect
when possible to avoid blocking visual updates.
Tip
If you’re migrating code from a class component, note
useLayoutEffect
fires in the same phase as
componentDidMount
and
componentDidUpdate
. However,
we recommend starting with
useEffect
first
and only trying
useLayoutEffect
if that causes a problem.
If you use server rendering, keep in mind that
neither
useLayoutEffect
nor
useEffect
can run until the JavaScript is downloaded. This is why React warns when a server-rendered component contains
useLayoutEffect
. To fix this, either move that logic to
useEffect
(if it isn’t necessary for the first render), or delay showing that component until after the client renders (if the HTML looks broken until
useLayoutEffect
runs).
To exclude a component that needs layout effects from the server-rendered HTML, render it conditionally with
showChild && <Child />
and defer showing it with
useEffect(() => { setShowChild(true); }, [])
. This way, the UI doesn’t appear broken before hydration.
useDebugValue
useDebugValue(value)
useDebugValue
can be used to display a label for custom hooks in React DevTools.
For example, consider the
useFriendStatus
custom Hook described in “Building Your Own Hooks”:
// Show a label in DevTools next to this Hook// e.g. "FriendStatus: Online"useDebugValue(isOnline ?'Online':'Offline'); return isOnline; }
Tip
We don’t recommend adding debug values to every custom Hook. It’s most valuable for custom Hooks that are part of shared libraries.
Defer formatting debug values
In some cases formatting a value for display might be an expensive operation. It’s also unnecessary unless a Hook is actually inspected.
For this reason
useDebugValue
accepts a formatting function as an optional second parameter. This function is only called if the Hooks are inspected. It receives the debug value as a parameter and should return a formatted display value.
For example a custom Hook that returned a
Date
value could avoid calling the
toDateString
function unnecessarily by passing the following formatter:
useDebugValue(date,date=> date.toDateString());
useDeferredValue
const deferredValue =useDeferredValue(value);
useDeferredValue
accepts a value and returns a new copy of the value that will defer to more urgent updates. If the current render is the result of an urgent update, like user input, React will return the previous value and then render the new value after the urgent render has completed.
This hook is similar to user-space hooks which use debouncing or throttling to defer updates. The benefits to using
useDeferredValue
is that React will work on the update as soon as other work finishes (instead of waiting for an arbitrary amount of time), and like
startTransition
, deferred values can suspend without triggering an unexpected fallback for existing content.
Memoizing deferred children
useDeferredValue
only defers the value that you pass to it. If you want to prevent a child component from re-rendering during an urgent update, you must also memoize that component with
React.memo
or
React.useMemo
:
// Memoizing tells React to only re-render when deferredQuery changes, // not when query changes. const suggestions =useMemo(()=> <SearchSuggestionsquery={deferredQuery}/>, [deferredQuery] );
Memoizing the children tells React that it only needs to re-render them when
deferredQuery
changes and not when
query
changes. This caveat is not unique to
useDeferredValue
, and it’s the same pattern you would use with similar hooks that use debouncing or throttling.
Updates in a transition yield to more urgent updates such as clicks.
Updates in a transition will not show a fallback for re-suspended content. This allows the user to continue interacting with the current content while rendering the update.
useId
const id =useId();
useId
is a hook for generating unique IDs that are stable across the server and client, while avoiding hydration mismatches.
Note
useId
is
not
for generating keys in a list. Keys should be generated from your data.
For a basic example, pass the
id
directly to the elements that need it:
functionCheckbox(){ const id =useId(); return( <> <labelhtmlFor={id}>Do you like React?</label> <inputid={id}type="checkbox"name="react"/> </> ); };
For multiple IDs in the same component, append a suffix using the same
id
:
useId
generates a string that includes the
:
token. This helps ensure that the token is unique, but is not supported in CSS selectors or APIs like
querySelectorAll
.
useId
supports an
identifierPrefix
to prevent collisions in multi-root apps. To configure, see the options for
hydrateRoot
and
ReactDOMServer
.
Library Hooks
The following Hooks are provided for library authors to integrate libraries deeply into the React model, and are not typically used in application code.
useSyncExternalStore
const state =useSyncExternalStore(subscribe, getSnapshot[, getServerSnapshot]);
useSyncExternalStore
is a hook recommended for reading and subscribing from external data sources in a way that’s compatible with concurrent rendering features like selective hydration and time slicing.
This method returns the value of the store and accepts three arguments:
subscribe
: function to register a callback that is called whenever the store changes.
getSnapshot
: function that returns the current value of the store.
getServerSnapshot
: function that returns the snapshot used during server rendering.
The most basic example simply subscribes to the entire store:
const state =useSyncExternalStore(store.subscribe, store.getSnapshot);
However, you can also subscribe to a specific field:
When server rendering, you must serialize the store value used on the server, and provide it to
useSyncExternalStore
. React will use this snapshot during hydration to prevent server mismatches:
getSnapshot
must return a cached value. If getSnapshot is called multiple times in a row, it must return the same exact value unless there was a store update in between.
A shim is provided for supporting multiple React versions published as
use-sync-external-store/shim
. This shim will prefer
useSyncExternalStore
when available, and fallback to a user-space implementation when it’s not.
As a convenience, we also provide a version of the API with automatic support for memoizing the result of getSnapshot published as
use-sync-external-store/with-selector
.
useInsertionEffect
useInsertionEffect(didUpdate);
The signature is identical to
useEffect
, but it fires synchronously
before
all DOM mutations. Use this to inject styles into the DOM before reading layout in
useLayoutEffect
. Since this hook is limited in scope, this hook does not have access to refs and cannot schedule updates.
Note:
useInsertionEffect
should be limited to css-in-js library authors. Prefer
useEffect
or
useLayoutEffect
instead.