← All React exercises

React quiz · 26

UseEffect() III

StatusNot taken

Question

What does the code snippet to the right output by console.log?

Code

exercise.tsx
1import * as React from "react";
2import { useState, useEffect } from "react";
3import { createRoot } from "react-dom/client";
4
5function App() {
6  const [count, setCount] = useState(1)
7  console.log(1)
8  useEffect(() => {
9    console.log(2)
10    return () => {
11      console.log(3)
12    }
13  }, [count])
14
15  useEffect(() => {
16    console.log(4)
17    setCount(count => count + 1)
18  }, [])
19  return <Child count={count} />
20}
21
22function Child({ count }) {
23  useEffect(() => {
24    console.log(5)
25    return () => {
26      console.log(6)
27    }
28  }, [count]);
29
30  return null;
31};
32
33const root = createRoot(document.getElementById('root'));
34root.render(<App/>)