'Synchronizing with Effects'·续3

label {
  display: block;
  margin-top: 20px;
  margin-bottom: 20px;
}

body {
  min-height: 150px;
}

Focus a field conditionally

This form renders two <MyInput /> components.

Press "Show form" and notice that the second field automatically gets focused. This is because both of the <MyInput /> components try to focus the field inside. When you call focus() for two input fields in a row, the last one always "wins".

Let's say you want to focus the first field. The first MyInput component now receives a boolean shouldFocus prop set to true. Change the logic so that focus() is only called if the shouldFocus prop received by MyInput is true.

```js src/MyInput.js active import { useEffect, useRef } from 'react';

export default function MyInput({ shouldFocus, value, onChange }) { const ref = useRef(null);

// TODO: call focus() only if shouldFocus is true. useEffect(() => { ref.current.focus(); }, []);

return ( ); }


```js src/App.js hidden
import { useState } from 'react';
import MyInput from './MyInput.js';

export default function Form() {
  const [show, setShow] = useState(false);
  const [firstName, setFirstName] = useState('Taylor');
  const [lastName, setLastName] = useState('Swift');
  const [upper, setUpper] = useState(false);
  const name = firstName + ' ' + lastName;
  return (
    <>
      <button onClick={() => setShow(s => !s)}>{show ? 'Hide' : 'Show'} form</button>
      <br />
      <hr />
      {show && (
        <>
          <label>
            Enter your first name:
            <MyInput
              value={firstName}
              onChange={e => setFirstName(e.target.value)}
              shouldFocus={true}
            />
          </label>
          <label>
            Enter your last name:
            <MyInput
              value={lastName}
              onChange={e => setLastName(e.target.value)}
              shouldFocus={false}
            />
          </label>
          <p>Hello, <b>{upper ? name.toUpperCase() : name}</b></p>
        </>
      )}
    </>
  );
}
label {
  display: block;
  margin-top: 20px;
  margin-bottom: 20px;
}

body {
  min-height: 150px;
}

To verify your solution, press "Show form" and "Hide form" repeatedly. When the form appears, only the first input should get focused. This is because the parent component renders the first input with shouldFocus={true} and the second input with shouldFocus={false}. Also check that both inputs still work and you can type into both of them.

You can't declare an Effect conditionally, but your Effect can include conditional logic.

Put the conditional logic inside the Effect. You will need to specify shouldFocus as a dependency because you are using it inside the Effect. (This means that if some input's shouldFocus changes from false to true, it will focus after mount.)

```js src/MyInput.js active import { useEffect, useRef } from 'react';

export default function MyInput({ shouldFocus, value, onChange }) { const ref = useRef(null);

useEffect(() => { if (shouldFocus) { ref.current.focus(); } }, [shouldFocus]);

return ( ); }


```js src/App.js hidden
import { useState } from 'react';
import MyInput from './MyInput.js';

export default function Form() {
  const [show, setShow] = useState(false);
  const [firstName, setFirstName] = useState('Taylor');
  const [lastName, setLastName] = useState('Swift');
  const [upper, setUpper] = useState(false);
  const name = firstName + ' ' + lastName;
  return (
    <>
      <button onClick={() => setShow(s => !s)}>{show ? 'Hide' : 'Show'} form</button>
      <br />
      <hr />
      {show && (
        <>
          <label>
            Enter your first name:
            <MyInput
              value={firstName}
              onChange={e => setFirstName(e.target.value)}
              shouldFocus={true}
            />
          </label>
          <label>
            Enter your last name:
            <MyInput
              value={lastName}
              onChange={e => setLastName(e.target.value)}
              shouldFocus={false}
            />
          </label>
          <p>Hello, <b>{upper ? name.toUpperCase() : name}</b></p>
        </>
      )}
    </>
  );
}
label {
  display: block;
  margin-top: 20px;
  margin-bottom: 20px;
}

body {
  min-height: 150px;
}

Fix an interval that fires twice

This Counter component displays a counter that should increment every second. On mount, it calls setInterval. This causes onTick to run every second. The onTick function increments the counter.

However, instead of incrementing once per second, it increments twice. Why is that? Find the cause of the bug and fix it.

Keep in mind that setInterval returns an interval ID, which you can pass to clearInterval to stop the interval.

```js src/Counter.js active import { useState, useEffect } from 'react';

export default function Counter() { const [count, setCount] = useState(0);

useEffect(() => { function onTick() { setCount(c => c + 1); }

setInterval(onTick, 1000); }, []);

return

{count}

; }


```js src/App.js hidden
import { useState } from 'react';
import Counter from './Counter.js';

export default function Form() {
  const [show, setShow] = useState(false);
  return (
    <>
      <button onClick={() => setShow(s => !s)}>{show ? 'Hide' : 'Show'} counter</button>
      <br />
      <hr />
      {show && <Counter />}
    </>
  );
}
label {
  display: block;
  margin-top: 20px;
  margin-bottom: 20px;
}

body {
  min-height: 150px;
}

When Strict Mode is on (like in the sandboxes on this site), React remounts each component once in development. This causes the interval to be set up twice, and this is why each second the counter increments twice.

However, React's behavior is not the cause of the bug: the bug already exists in the code. React's behavior makes the bug more noticeable. The real cause is that this Effect starts a process but doesn't provide a way to clean it up.

To fix this code, save the interval ID returned by setInterval, and implement a cleanup function with clearInterval:

```js src/Counter.js active import { useState, useEffect } from 'react';

export default function Counter() { const [count, setCount] = useState(0);

useEffect(() => { function onTick() { setCount(c => c + 1); }

const intervalId = setInterval(onTick, 1000); return () => clearInterval(intervalId); }, []);

return

{count}

; }


```js src/App.js hidden
import { useState } from 'react';
import Counter from './Counter.js';

export default function App() {
  const [show, setShow] = useState(false);
  return (
    <>
      <button onClick={() => setShow(s => !s)}>{show ? 'Hide' : 'Show'} counter</button>
      <br />
      <hr />
      {show && <Counter />}
    </>
  );
}
label {
  display: block;
  margin-top: 20px;
  margin-bottom: 20px;
}

body {
  min-height: 150px;
}

In development, React will still remount your component once to verify that you've implemented cleanup well. So there will be a setInterval call, immediately followed by clearInterval, and setInterval again. In production, there will be only one setInterval call. The user-visible behavior in both cases is the same: the counter increments once per second.

Fix fetching inside an Effect

This component shows the biography for the selected person. It loads the biography by calling an asynchronous function fetchBio(person) on mount and whenever person changes. That asynchronous function returns a Promise which eventually resolves to a string. When fetching is done, it calls setBio to display that string under the select box.

```js {expectedErrors: {'react-compiler': [9]}} src/App.js import { useState, useEffect } from 'react'; import { fetchBio } from './api.js';

export default function Page() { const [person, setPerson] = useState('Alice'); const [bio, setBio] = useState(null);

useEffect(() => { setBio(null); fetchBio(person).then(result => { setBio(result); }); }, [person]);

return ( <>


{bio ?? 'Loading...'}

</> ); }


```js src/api.js hidden
export async function fetchBio(person) {
  const delay = person === 'Bob' ? 2000 : 200;
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('This is ' + person + '’s bio.');
    }, delay);
  })
}

There is a bug in this code. Start by selecting "Alice". Then select "Bob" and then immediately after that select "Taylor". If you do this fast enough, you will notice that bug: Taylor is selected, but the paragraph below says "This is Bob's bio."

Why does this happen? Fix the bug inside this Effect.

If an Effect fetches something asynchronously, it usually needs cleanup.

To trigger the bug, things need to happen in this order:

  • Selecting 'Bob' triggers fetchBio('Bob')
  • Selecting 'Taylor' triggers fetchBio('Taylor')
  • Fetching 'Taylor' completes before fetching 'Bob'
  • The Effect from the 'Taylor' render calls setBio('This is Taylor’s bio')
  • Fetching 'Bob' completes
  • The Effect from the 'Bob' render calls setBio('This is Bob’s bio')

This is why you see Bob's bio even though Taylor is selected. Bugs like this are called race conditions because two asynchronous operations are "racing" with each other, and they might arrive in an unexpected order.

To fix this race condition, add a cleanup function:

```js {expectedErrors: {'react-compiler': [9]}} src/App.js import { useState, useEffect } from 'react'; import { fetchBio } from './api.js';

export default function Page() { const [person, setPerson] = useState('Alice'); const [bio, setBio] = useState(null); useEffect(() => { let ignore = false; setBio(null); fetchBio(person).then(result => { if (!ignore) { setBio(result); } }); return () => { ignore = true; } }, [person]);

return ( <>


{bio ?? 'Loading...'}

</> ); }


```js src/api.js hidden
export async function fetchBio(person) {
  const delay = person === 'Bob' ? 2000 : 200;
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('This is ' + person + '’s bio.');
    }, delay);
  })
}

Each render's Effect has its own ignore variable. Initially, the ignore variable is set to false. However, if an Effect gets cleaned up (such as when you select a different person), its ignore variable becomes true. So now it doesn't matter in which order the requests complete. Only the last person's Effect will have ignore set to false, so it will call setBio(result). Past Effects have been cleaned up, so the if (!ignore) check will prevent them from calling setBio:

  • Selecting 'Bob' triggers fetchBio('Bob')
  • Selecting 'Taylor' triggers fetchBio('Taylor') and cleans up the previous (Bob's) Effect
  • Fetching 'Taylor' completes before fetching 'Bob'
  • The Effect from the 'Taylor' render calls setBio('This is Taylor’s bio')
  • Fetching 'Bob' completes
  • The Effect from the 'Bob' render does not do anything because its ignore flag was set to true

In addition to ignoring the result of an outdated API call, you can also use AbortController to cancel the requests that are no longer needed. However, by itself this is not enough to protect against race conditions. More asynchronous steps could be chained after the fetch, so using an explicit flag like ignore is the most reliable way to fix this type of problem.


Sitemap

Overview of all docs pages