Escape Hatches - React
Escape Hatches - React
Managing State
Escape Hatches
Escape Hatches
ADVANCED
Like state, refs are retained by React between re-renders. However, setting
state re-renders a component. Changing a ref does not! You can access the
current value of that ref through the ref.current property.
https://react.dev/learn/escape-hatches 2/25
2/1/24, 5:17 AM Escape Hatches – React
function handleClick() {
ref.current = ref.current + 1;
alert('You clicked ' + ref.current + ' times!');
}
t (
Show more
Click me!
A ref is like a secret pocket of your component that React doesn’t track. For
example, you can use refs to store timeout IDs, DOM elements, and other
objects that don’t impact the component’s rendering output.
https://react.dev/learn/escape-hatches 3/25
2/1/24, 5:17 AM Escape Hatches – React
Read More
https://react.dev/learn/escape-hatches 4/25
2/1/24, 5:17 AM Escape Hatches – React
function handleClick() {
inputRef.current.focus();
}
return (
<>
Show more
https://react.dev/learn/escape-hatches 5/25
2/1/24, 5:17 AM Escape Hatches – React
Read More
Press Play/Pause a few times and see how the video player stays
synchronized to the isPlaying prop value:
Is this page useful?
https://react.dev/learn/escape-hatches 6/25
2/1/24, 5:17 AM Escape Hatches – React
useEffect(() => {
if (isPlaying) {
ref.current.play();
} else {
ref.current.pause();
}
Show more
https://react.dev/learn/escape-hatches 7/25
2/1/24, 5:17 AM Escape Hatches – React
Many Effects also “clean up” after themselves. For example, an Effect that
sets up a connection to a chat server should return a cleanup function that
tells React how to disconnect your component from that server:
https://react.dev/learn/escape-hatches 8/25
2/1/24, 5:17 AM Escape Hatches – React
In development, React will immediately run and clean up your Effect one extra
time. This is why you see " ✅ Connecting..." printed twice. This ensures
that you don’t forget to implement the cleanup function.
Read More
https://react.dev/learn/escape-hatches 9/25
2/1/24, 5:17 AM Escape Hatches – React
update a component’s state when some props or state change), you shouldn’t
need an Effect. Removing unnecessary Effects will make your code easier to
follow, faster to run, and less error-prone.
There are two common cases in which you don’t need Effects:
For example, you don’t need an Effect to adjust some state based on other
state:
function Form() {
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
https://react.dev/learn/escape-hatches 10/25
2/1/24, 5:17 AM Escape Hatches – React
function Form() {
const [firstName, setFirstName] = useState('Taylor');
const [lastName, setLastName] = useState('Swift');
// ✅ Good: calculated during rendering
const fullName = firstName + ' ' + lastName;
// ...
}
Read More
This Effect depends on the value of the roomId prop. Props are reactive
values, which means they can change on a re-render. Notice that the Effect re-
synchronizes (and re-connects to the server) if roomId changes:
Show more
https://react.dev/learn/escape-hatches 12/25
2/1/24, 5:17 AM Escape Hatches – React
React provides a linter rule to check that you’ve specified your Effect’s
dependencies correctly. If you forget to specify roomId in the list of
dependencies in the above example, the linter will find that bug automatically.
Read More
https://react.dev/learn/escape-hatches 13/25
2/1/24, 5:17 AM Escape Hatches – React
Under Construction
This section describes an experimental API that has not yet been
released in a stable version of React.
Event handlers only re-run when you perform the same interaction again.
Unlike event handlers, Effects re-synchronize if any of the values they read,
like props or state, are different than during last render. Sometimes, you want
a mix of both behaviors: an Effect that re-runs in response to some values but
not others.
All code inside Effects is reactive. It will run again if some reactive value it
reads has changed due to a re-render. For example, this Effect will re-connect
to the chat if either roomId or theme have changed:
Show more
This is not ideal. You want to re-connect to the chat only if the roomId has
changed. Switching the theme shouldn’t re-connect to the chat! Move the
code reading theme out of your Effect into an Effect Event:
https://react.dev/learn/escape-hatches 15/25
2/1/24, 5:17 AM Escape Hatches – React
Show more
https://react.dev/learn/escape-hatches 16/25
2/1/24, 5:17 AM Escape Hatches – React
Code inside Effect Events isn’t reactive, so changing the theme no longer
makes your Effect re-connect.
Read More
For example, this Effect depends on the options object which gets re-
created every time you edit the input:
const options = {
serverUrl: serverUrl,
roomId: roomId
Show more
https://react.dev/learn/escape-hatches 18/25
2/1/24, 5:17 AM Escape Hatches – React
You don’t want the chat to re-connect every time you start typing a message
in that chat. To fix this problem, move creation of the options object inside
the Effect so that the Effect only depends on the roomId string:
useEffect(() => {
const options = {
serverUrl: serverUrl,
Show more
https://react.dev/learn/escape-hatches 19/25
2/1/24, 5:17 AM Escape Hatches – React
Notice that you didn’t start by editing the dependency list to remove the
options dependency. That would be wrong. Instead, you changed the
Read More
https://react.dev/learn/escape-hatches 20/25
2/1/24, 5:17 AM Escape Hatches – React
https://react.dev/learn/escape-hatches 21/25
2/1/24, 5:17 AM Escape Hatches – React
<>
Show more
You can create custom Hooks, compose them together, pass data between
them, and reuse them between components. As your app grows, you will write
fewer Effects by hand because you’ll be able to reuse custom Hooks you
already wrote. There are also many excellent custom Hooks maintained by the
React community.
https://react.dev/learn/escape-hatches 22/25
2/1/24, 5:17 AM Escape Hatches – React
Read Reusing Logic with Custom Hooks to learn how to share logic
between components.
Read More
What’s next?
Head over to Referencing Values with Refs to start reading this chapter page
by page!
PREVIOUS NEXT
https://react.dev/learn/escape-hatches 23/25
2/1/24, 5:17 AM Escape Hatches – React
https://react.dev/learn/escape-hatches 24/25
2/1/24, 5:17 AM Escape Hatches – React
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/learn/escape-hatches 25/25