Consider this React component that uses two useEffect hooks. What will be logged to the console when the component first renders?
import React, { useState, useEffect } from 'react'; function Example() { const [count, setCount] = useState(0); useEffect(() => { console.log('Effect A:', count); }, [count]); useEffect(() => { console.log('Effect B: Component mounted'); }, []); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
Remember that effects with empty dependency arrays run once after the first render, and effects with dependencies run after render and when dependencies change.
The effects run in the order they are defined after the first render. The first effect (Effect A) depends on count and logs Effect A: 0 since count is initialized to 0. The second effect (Effect B) has an empty dependency array and logs Effect B: Component mounted.
Given this React component with two useEffect hooks, what will be the value of count displayed after clicking the button two times?
import React, { useState, useEffect } from 'react'; function Counter() { const [count, setCount] = useState(0); useEffect(() => { setCount(c => c + 1); }, []); useEffect(() => { console.log('Count changed:', count); }, [count]); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; }
Think about how the initial effect increments count once, then each button click increments count by one.
Initially, count is 0. The first useEffect runs once after mount and increments count to 1. Then clicking the button twice increments count by 2 more, resulting in 3.
In this component, what is the order of console logs when count changes from 0 to 1?
import React, { useState, useEffect } from 'react'; function Demo() { const [count, setCount] = useState(0); useEffect(() => { console.log('Effect setup:', count); return () => { console.log('Effect cleanup:', count); }; }, [count]); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; }
Recall that React runs cleanup of the previous effect before running the new effect.
When count changes, React first runs the cleanup function of the previous effect (logging Effect cleanup: 0), then runs the new effect setup (logging Effect setup: 1).
Which of these useEffect hooks will cause a runtime error or infinite loop?
Consider what happens if you update state inside an effect that depends on that state.
Option A updates count inside an effect that depends on count. This causes the effect to run repeatedly, creating an infinite loop and runtime error.
Examine this React component. Why does the document title not update when count changes?
import React, { useState, useEffect } from 'react'; function TitleUpdater() { const [count, setCount] = useState(0); useEffect(() => { document.title = `Count is ${count}`; }, []); return <button onClick={() => setCount(count + 1)}>Increment</button>; }
Think about how dependency arrays control when effects run.
The effect runs only once after the first render because of the empty dependency array. It sets the title to 'Count is 0' and never updates it again when count changes.