class-to-hooks

Convert React class components to functional components using hooks.

Updated Jan 17, 2026
One-click install
npx skills add https://github.com/saddam-eng-tech/ai-agent-skills --skill class-to-hooks
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: class-to-hooks
Source: https://github.com/saddam-eng-tech/ai-agent-skills/tree/main/class-to-hooks
Command: npx skills add https://github.com/saddam-eng-tech/ai-agent-skills --skill class-to-hooks

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill automates the conversion of legacy React class components into modern functional components using hooks, reducing technical debt and improving code maintainability.

Core Features & Use Cases

  • Lifecycle Method Mapping: Accurately translates componentDidMount, componentDidUpdate, and componentWillUnmount to useEffect patterns.
  • State Management Conversion: Transforms this.state and this.setState into useState and its setter functions.
  • Instance Method Refactoring: Converts class methods into functional components, using useCallback where necessary.
  • Ref and Context Handling: Migrates createRef to useRef and contextType to useContext.
  • Use Case: Refactor an older React codebase to leverage functional components and hooks, making it easier to work with and test.

Quick Start

Convert the provided React class component to a functional component using hooks.

Frequently Asked Questions about class-to-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I convert a React class component to functional hooks?

React class components are refactored to functional components by mapping this.state to useState, lifecycle methods like componentDidMount to useEffect, instance methods to useCallback, and refs to useRef. This reduces technical debt and improves code maintainability.

How do I map componentDidUpdate to useEffect dependency arrays?

Mapping componentDidUpdate to useEffect dependency arrays involves translating previous props and state comparisons into array values. The conversion process handles these complex scenarios to ensure the hook triggers accurately when the tracked dependencies change.

Can I use this to refactor error boundaries in React?

Error boundaries cannot be refactored using hooks. Error boundaries require specific class component lifecycle methods that functional components cannot replicate, so the refactoring process flags these unconvertible patterns rather than attempting an automatic translation.

What is the best way to handle contextType when converting to hooks?

Handling contextType when converting to hooks requires mapping it to useContext. This replaces the static class property with a direct hook call inside the functional component, ensuring proper re-renders when the React context value changes.

How do instance methods translate when converting React class to hooks?

Instance methods translate to standard functions or useCallback when converting React class to hooks. Using useCallback is necessary where the method is passed as a prop to child components, preventing unnecessary re-renders by maintaining referential equality across renders.

Does this component conversion support TypeScript React codebases?

Yes, this component conversion supports TypeScript React codebases. It handles both JavaScript and TypeScript refactoring to leverage functional components and hooks, making older codebases easier to work with and test while transforming legacy class patterns.