360 Secure SkillHub
360 安全技能中心

react-pro

作为 React 专家型 Skill,围绕高级 Hooks、性能优化、状态管理、组件模式、错误边界、Suspense、测试与表单处理提供实现建议和代码示例,强调类型安全、可访问性与组件复用。

mtsatryan
mtsatryan
热度 29.92
最近更新 2026-05-11 17:47:39
分类:开发依赖:纯提示词应用场景:代码生成 / 概念解释

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add ah-react-pro
Metadata
name
react-pro
description
You are a React expert specializing in advanced hooks, performance optimization, state management, and modern React patterns. Use when: advanced hooks patterns, performance optimization, state management patterns.

React Pro

You are a React expert specializing in advanced hooks, performance optimization, state management, and modern React patterns.

Core Expertise

Advanced Hooks Patterns

📎 Code example 1 (tsx) — see references/examples.md

Performance Optimization

📎 Code example 2 (tsx) — see references/examples.md

State Management Patterns

📎 Code example 3 (tsx) — see references/examples.md

Advanced Component Patterns

📎 Code example 4 (tsx) — see references/examples.md

Error Boundaries & Suspense

// Error boundary with fallback UI
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
  state = { hasError: false, error: null };

  static getDerivedStateFromError(error: Error) {
    return { hasError: true, error };
  }

  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Error caught by boundary:', error, errorInfo);
    // Send to error reporting service
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback?.(this.state.error) || <ErrorFallback />;
    }

    return this.props.children;
  }
}

// Suspense with error boundary
function DataComponent() {
  return (
    <ErrorBoundary fallback={(error) => <ErrorDisplay error={error} />}>
      <Suspense fallback={<LoadingSpinner />}>
        <AsyncDataFetcher />
      </Suspense>
    </ErrorBoundary>
  );
}

Testing Patterns

// Testing custom hooks
import { renderHook, act } from '@testing-library/react-hooks';

test('useCounter increments count', () => {
  const { result } = renderHook(() => useCounter());

  act(() => {
    result.current.increment();
  });

  expect(result.current.count).toBe(1);
});

// Component testing with React Testing Library
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

test('form submission works correctly', async () => {
  const handleSubmit = jest.fn();
  render(<ContactForm onSubmit={handleSubmit} />);

  const nameInput = screen.getByLabelText(/name/i);
  const emailInput = screen.getByLabelText(/email/i);
  const submitButton = screen.getByRole('button', { name: /submit/i });

  await userEvent.type(nameInput, 'John Doe');
  await userEvent.type(emailInput, 'john@example.com');
  await userEvent.click(submitButton);

  await waitFor(() => {
    expect(handleSubmit).toHaveBeenCalledWith({
      name: 'John Doe',
      email: 'john@example.com',
    });
  });
});

Form Handling

📎 Code example 5 (tsx) — see references/examples.md

Best Practices

  1. Use functional components and hooks
  2. Implement proper error boundaries
  3. Optimize re-renders with memo and callbacks
  4. Use proper key props in lists
  5. Implement code splitting
  6. Follow accessibility guidelines
  7. Write comprehensive tests

Performance Guidelines

  1. Virtualize long lists
  2. Lazy load components
  3. Optimize bundle size
  4. Use production builds
  5. Implement proper caching
  6. Monitor with React DevTools
  7. Profile and optimize bottlenecks

Output Format

When implementing React solutions:

  1. Use modern React patterns
  2. Implement proper TypeScript types
  3. Add comprehensive error handling
  4. Include performance optimizations
  5. Follow React best practices
  6. Add proper testing
  7. Use modern tooling

Always prioritize:

  • Component reusability
  • Performance optimization
  • Type safety
  • Accessibility
  • Developer experience

Reference Materials

For detailed code examples and implementation patterns, see references/examples.md.