React Class Lifecycle: Still Asked in Interviews
⚛ ری‌اکت

React Class Lifecycle: Still Asked in Interviews

Mounting, updating, unmounting phases — and how they map to hooks in modern React.

BugCast Admin··۵ دقیقه مطالعه

Even in the hooks era, interviewers ask about class lifecycle methods to see if you understand when side effects belong. Know the mapping to useEffect and friends.

Common Interview Questions

Q: Name the three lifecycle phases

Answer: Mounting (first render), updating (props/state change), and unmounting (component removed). Each has associated methods like componentDidMount, componentDidUpdate, and componentWillUnmount.

Q: componentDidMount vs useEffect empty deps?

Answer: Both run after paint. componentDidMount fires once after first mount. useEffect(() => {}, []) runs after first render and is the hooks equivalent for mount-only effects.

Q: Why was componentWillMount deprecated?

Answer: Async rendering and Strict Mode made it unsafe for side effects and subscriptions. Data fetching moved to componentDidMount or later to hooks/useEffect.

Q: getDerivedStateFromProps use case?

Answer: Rare — sync state derived from props when you need local state mirroring props changes (e.g., controlled input reset). Prefer fully controlled components or keys to reset state.

Key Points for Interviews

  • Map lifecycle to hooks for modern answers

  • Fetch data in mount effect, not render

  • Cleanup subscriptions in unmount

  • getSnapshotBeforeUpdate for DOM measurements before paint

  • Strict Mode double-invokes effects in development

#lifecycle#class-components#interview

مطالب مرتبط