react-component-architecture
About
This skill helps developers design scalable React components using functional components, hooks, and composition patterns with TypeScript. It provides guidance for building reusable component libraries and maintainable UI systems. Use it when creating large-scale applications or developing custom hooks and performance-optimized UI patterns.
Documentation
React Component Architecture
Overview
Build scalable, maintainable React components using modern patterns including functional components, hooks, composition, and TypeScript for type safety.
When to Use
- Component library design
- Large-scale React applications
- Reusable UI patterns
- Custom hooks development
- Performance optimization
Implementation Examples
1. Functional Component with Hooks
// Button.tsx
import React, { useState, useCallback } from 'react';
interface ButtonProps {
variant?: 'primary' | 'secondary' | 'danger';
size?: 'sm' | 'md' | 'lg';
disabled?: boolean;
onClick?: () => void;
children: React.ReactNode;
}
export const Button: React.FC<ButtonProps> = ({
variant = 'primary',
size = 'md',
disabled = false,
onClick,
children
}) => {
const variantStyles = {
primary: 'bg-blue-500 hover:bg-blue-600',
secondary: 'bg-gray-500 hover:bg-gray-600',
danger: 'bg-red-500 hover:bg-red-600'
};
const sizeStyles = {
sm: 'px-2 py-1 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg'
};
return (
<button
className={`${variantStyles[variant]} ${sizeStyles[size]} text-white rounded disabled:opacity-50`}
disabled={disabled}
onClick={onClick}
>
{children}
</button>
);
};
2. Custom Hooks Pattern
// useFormInput.ts
import { useState, useCallback } from 'react';
interface UseFormInputOptions {
initialValue?: string;
validator?: (value: string) => string | null;
}
export const useFormInput = (options: UseFormInputOptions = {}) => {
const [value, setValue] = useState(options.initialValue || '');
const [error, setError] = useState<string | null>(null);
const validate = useCallback(() => {
if (options.validator) {
const validationError = options.validator(value);
setError(validationError);
return !validationError;
}
return true;
}, [value, options.validator]);
const reset = useCallback(() => {
setValue(options.initialValue || '');
setError(null);
}, [options.initialValue]);
return {
value,
setValue,
error,
validate,
reset,
bind: {
value,
onChange: (e: React.ChangeEvent<HTMLInputElement>) => setValue(e.target.value)
}
};
};
// Usage
const MyForm: React.FC = () => {
const email = useFormInput({
validator: (v) => !v.includes('@') ? 'Invalid email' : null
});
return (
<div>
<input {...email.bind} />
{email.error && <span className="text-red-500">{email.error}</span>}
</div>
);
};
3. Composition Pattern
// Card.tsx
interface CardProps {
children: React.ReactNode;
className?: string;
}
const Card: React.FC<CardProps> = ({ children, className = '' }) => (
<div className={`border rounded p-4 ${className}`}>{children}</div>
);
const CardHeader: React.FC<CardProps> = ({ children }) => (
<div className="border-b pb-2 mb-3 font-bold">{children}</div>
);
const CardBody: React.FC<CardProps> = ({ children }) => (
<div className="py-2">{children}</div>
);
const CardFooter: React.FC<CardProps> = ({ children }) => (
<div className="border-t pt-2 mt-3">{children}</div>
);
// Compound component
export { Card };
Card.Header = CardHeader;
Card.Body = CardBody;
Card.Footer = CardFooter;
// Usage
<Card>
<Card.Header>Title</Card.Header>
<Card.Body>Content</Card.Body>
<Card.Footer>Actions</Card.Footer>
</Card>
4. Higher-Order Component (HOC)
// withLoader.tsx
interface WithLoaderProps {
isLoading: boolean;
error?: Error | null;
}
function withLoader<P extends object>(
Component: React.ComponentType<P>
): React.FC<P & WithLoaderProps> {
return ({ isLoading, error, ...props }: P & WithLoaderProps) => {
if (isLoading) return <div>Loading...</div>;
if (error) return <div className="text-red-500">{error.message}</div>;
return <Component {...(props as P)} />;
};
}
// Usage
const UserList: React.FC<{ users: User[] }> = ({ users }) => (
<ul>{users.map(u => <li key={u.id}>{u.name}</li>)}</ul>
);
export const LoadingUserList = withLoader(UserList);
5. Render Props Pattern
// DataFetcher.tsx
interface DataFetcherProps<T> {
url: string;
children: (data: T | null, loading: boolean, error: Error | null) => React.ReactNode;
}
export const DataFetcher = <T,>({ url, children }: DataFetcherProps<T>) => {
const [data, setData] = React.useState<T | null>(null);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState<Error | null>(null);
React.useEffect(() => {
fetch(url)
.then(r => r.json())
.then(setData)
.catch(setError)
.finally(() => setLoading(false));
}, [url]);
return <>{children(data, loading, error)}</>;
};
// Usage
<DataFetcher<User[]> url="/api/users">
{(users, loading, error) => (
<>{loading ? <p>Loading...</p> : users?.map(u => <p key={u.id}>{u.name}</p>)}</>
)}
</DataFetcher>
Best Practices
- Use TypeScript for type safety
- Implement proper prop validation
- Keep components focused and single-purpose
- Leverage hooks for state and side effects
- Use composition over inheritance
- Memoize expensive computations
- Extract custom hooks for reusable logic
Resources
Quick Install
/plugin add https://github.com/aj-geddes/useful-ai-prompts/tree/main/react-component-architectureCopy and paste this command in Claude Code to install this skill
GitHub 仓库
Related Skills
sglang
MetaSGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.
evaluating-llms-harness
TestingThis Claude Skill runs the lm-evaluation-harness to benchmark LLMs across 60+ standardized academic tasks like MMLU and GSM8K. It's designed for developers to compare model quality, track training progress, or report academic results. The tool supports various backends including HuggingFace and vLLM models.
llamaguard
OtherLlamaGuard is Meta's 7-8B parameter model for moderating LLM inputs and outputs across six safety categories like violence and hate speech. It offers 94-95% accuracy and can be deployed using vLLM, Hugging Face, or Amazon SageMaker. Use this skill to easily integrate content filtering and safety guardrails into your AI applications.
langchain
MetaLangChain is a framework for building LLM applications using agents, chains, and RAG pipelines. It supports multiple LLM providers, offers 500+ integrations, and includes features like tool calling and memory management. Use it for rapid prototyping and deploying production systems like chatbots, autonomous agents, and question-answering services.
