VVersions.dev

Migrate React class components to hooks

Converting class components to hooks improves readability and unlocks modern React features. Do it incrementally, component by component, with tests as the safety net.

Pattern modernizationDifficulty: moderatelow risk

Last verified · Updated May 22, 2026

Converting class components to function components with hooks is a low-risk, incremental modernization. Map lifecycle methods to effects, this.state to useState/useReducer, and convert one component at a time behind passing tests.

Transformation rules

Class patternHook equivalent
this.state / setStateuseState or useReducer
componentDidMountuseEffect(fn, [])
componentDidUpdateuseEffect(fn, [deps])
componentWillUnmountuseEffect cleanup return
this.contextuseContext

When the pattern applies

  • Components with simple-to-moderate lifecycle logic.
  • Components you are already touching for an upgrade.

Edge cases

  • getDerivedStateFromProps rarely maps cleanly — reconsider the data flow.
  • Error boundaries must remain class components (no hook equivalent yet).
  • Beware effect dependency arrays re-running side effects.