react-class-to-functional

Convert React class components to functional components with hooks.

32.4k|4.1k|Updated Feb 6, 2014
One-click install
npx skills add https://github.com/cockroachdb/cockroach --skill react-class-to-functional
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-class-to-functional
Source: https://github.com/cockroachdb/cockroach/tree/main/pkg/ui/.claude/skills/react-class-to-functional
Command: npx skills add https://github.com/cockroachdb/cockroach --skill react-class-to-functional

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill automates the tedious and error-prone process of converting legacy React class components into modern functional components using hooks, improving code readability and maintainability.

Core Features & Use Cases

  • State Conversion: Transforms this.state and this.setState into useState hooks.
  • Lifecycle Hook Mapping: Converts componentDidMount, componentDidUpdate, and componentWillUnmount into useEffect hooks.
  • Ref and Context Handling: Migrates createRef to useRef and static contextType to useContext.
  • Use Case: Refactor a large React application's component library from class-based to functional components to leverage hooks for better state management and side-effect handling.

Quick Start

Use the react-class-to-functional skill to convert the file '/path/to/MyClassComponent.tsx'.

Frequently Asked Questions about react-class-to-functional

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

FAQPage Schema
How do I convert React class components to functional components with hooks?

Converting React class components to functional components is automated by transforming `this.state` into `useState` and mapping lifecycle methods like `componentDidMount` to `useEffect`. This refactoring improves code readability and maintainability by leveraging modern hooks for state and side-effect handling.

What is the best way to migrate React lifecycle methods to useEffect?

Migrating React lifecycle methods to `useEffect` involves mapping `componentDidMount`, `componentDidUpdate`, and `componentWillUnmount` into functional effect hooks. This process replaces class-based side-effect management with modern hook patterns, ensuring proper cleanup and dependency tracking for improved component maintainability.

Can I preserve TypeScript types when converting React class components to functional components?

TypeScript type preservation is supported during React class to functional component conversion. The refactoring process handles generic component patterns and ensures that existing TypeScript definitions remain intact, allowing you to modernize legacy codebases without losing static type safety or altering component interfaces.

Does React refactoring handle context and refs migration?

React refactoring handles context and refs by migrating `createRef` to `useRef` and `static contextType` to `useContext`. This ensures that functional components maintain the same ref forwarding and context consumption capabilities as their class-based predecessors during the modernization process.

What are the limitations of automating React class to functional component conversion?

Automating React class to functional component conversion handles state, lifecycle, refs, and context, but complex generic component patterns may require manual review. The Skill streamlines the bulk transformation, though developers should verify hook dependency arrays and edge-case side-effect logic post-conversion.