← All React exercises

React quiz · 31

UseRef II

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, useRef} from 'react'
3import { createRoot } from 'react-dom/client'
4
5function App() {
6  const ref = useRef()
7  
8  console.log(!!ref.current)
9  useEffect(() => {
10    console.log(!!ref.current)
11  }, [ref.current]) 
12
13  const [state, setState] = useState(0)
14  console.log(1)
15  
16  useEffect(() => {
17    console.log(2)
18    setState(state => state + 1)
19  }, [])
20
21  return <div ref={ref}/>
22}
23
24const root = createRoot(document.getElementById('root'));
25root.render(<App/>)