Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Migrating class components to hooks

Convert React class components to function components with hooks: map each lifecycle method, setState and this to useState and useEffect, and know what stays a class.

Class components and function components with hooks can do almost the same things, and both are supported in React 19. Migrating is optional, but hooks make logic easier to share and often shorter. The trick is not to translate lifecycle methods one by one. Hooks are organized around what the code does (synchronize something with props or state), not when it runs (mount, update, unmount).

Migrate one component at a time. Class and function components mix freely in the same tree, so there is no need for a big-bang rewrite.

JSX
class Clock extends React.Component {
  state = { now: 0 };
  componentDidMount() { this.id = setInterval(this.tick, 1000); }
  componentWillUnmount() { clearInterval(this.id); }
}
// becomes a state variable plus one effect that sets up and cleans up

Try it

The same counter, class and hooks

Click each counter and toggle both away, then compare the console lines. The hook version has one effect that describes "keep the title equal to the count". It handles mount, change and unmount without separate methods.

State merging versus separate variables

Lifecycle map

Class feature Hooks equivalent
this.state and this.setState useState or useReducer; setState merges objects, useState replaces
Constructor setting state Initial value (or lazy initializer)
componentDidMount useEffect with [] dependencies
componentDidUpdate useEffect with the relevant dependencies
componentWillUnmount The cleanup function returned from useEffect
Mount + update + unmount for one concern One useEffect with cleanup and dependencies
shouldComponentUpdate, PureComponent memo
getDerivedStateFromProps Compute during render, or reset with a key
getSnapshotBeforeUpdate useLayoutEffect can read the DOM before paint; no exact equivalent
createRef, this.refs useRef
static contextType, Consumer useContext
Instance fields (this.timer) useRef for mutable values that do not trigger renders
componentDidCatch, getDerivedStateFromError No hook yet: keep a class (see error boundaries)

Differences that cause bugs

  • Function components capture values. A class reads this.props and this.state at the moment code runs, so a delayed callback sees the latest values. A function component's callback sees the values of the render that created it (see Stale closures). Use functional updates (setCount(c => c + 1)) or a ref for the latest value.
  • setState merges, useState replaces. When you store an object, spread the old one: setForm({ ...form, name }).
  • Effects run after paint (use useLayoutEffect only to measure or avoid flicker), while componentDidMount ran before the browser painted.
  • Dependencies are mandatory thinking. An effect with a missing dependency reads stale data; an effect that does too much re-runs too often. Split effects by concern.
  • No this. Methods become functions inside the component. Pass handlers directly; memoize with useCallback only when needed.
  • Strict Mode runs effects twice in development (set up, clean up, set up) to prove your cleanup works. Classes did not have this check.

Usage notes

  • Extract custom hooks to replace mixins and higher-order components; see Rules of Hooks.
  • Migrate leaf components first, then work up the tree; error boundaries can remain classes indefinitely.
  • Do not port componentWillReceiveProps-style logic into an effect that copies props to state. Derive values in render instead (see Derived state).
  • Write tests first if the component has none, and keep them unchanged across the migration.