Interview Questions · ReactJs ReactJs

In a function component, which useEffect dependency-array configuration most closely mirrors the behavior of componentDidMount from class components?

Practice question from Interview Questions (ReactJs ReactJs), with the correct answer and a step-by-step solution.

medium · MCQ · Q-09146A

In a function component, which useEffect dependency-array configuration most closely mirrors the behavior of componentDidMount from class components?

Figure 1 for the question
  1. A.useEffect(fn) with no dependency array at all
  2. B.useEffect(fn, []) with an empty dependency arrayCorrect answer
  3. C.useEffect(fn, [props, state]) listing every prop and state variable
  4. D.useLayoutEffect(fn, []) is the only way to replicate componentDidMount; useEffect cannot do this

Solution

Recall which array configuration signals to React 'this effect has no reactive values it depends on, so run it only once.'

More from Interview Questions

All of Interview Questions

Practise more ReactJs

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed