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?
- A.useEffect(fn) with no dependency array at all
- B.useEffect(fn, []) with an empty dependency arrayCorrect answer
- C.useEffect(fn, [props, state]) listing every prop and state variable
- 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
- What is a Higher-Order Component (HOC) in React?
- Why does React use a Virtual DOM instead of directly manipulating the real DOM on every update?
- What problem does the React Context API primarily solve?
- What distinguishes a controlled component from an uncontrolled component in React forms?
Practise more ReactJs
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed