Get the FREE Ultimate OpenClaw Setup Guide →

react-patterns

npx machina-cli add skill shahtuyakov/claude-setup/react-patterns --openclaw
Files (1)
SKILL.md
3.3 KB

React Patterns

Modern React and Next.js patterns with TypeScript.

Framework Selection

SetupBest ForUse When
Next.js App RouterFull-stack, SSR/SSGSEO matters, need server components
Next.js Pages RouterSimpler SSRLegacy project, simpler mental model
Vite + ReactSPA, client-onlyNo SSR needed, fastest dev experience
Create React AppLegacyAvoid for new projects

Reference Files

TopicLoadUse When
Project structurereferences/project-structure.mdSetting up or organizing code
Componentsreferences/components.mdBuilding UI components
State managementreferences/state-management.mdManaging app/component state
Data fetchingreferences/data-fetching.mdAPI calls, server data
Formsreferences/forms.mdForm handling and validation
Auth (client)references/auth-client.mdClient-side authentication

Quick Start Patterns

Next.js App Router Page

// app/users/page.tsx
import { getUsers } from '@/lib/api';
import { UserList } from '@/components/users/UserList';

export default async function UsersPage() {
  const users = await getUsers();

  return (
    <main className="container mx-auto py-8">
      <h1 className="text-2xl font-bold mb-6">Users</h1>
      <UserList users={users} />
    </main>
  );
}

Client Component

'use client';

import { useState } from 'react';

interface CounterProps {
  initialCount?: number;
}

export function Counter({ initialCount = 0 }: CounterProps) {
  const [count, setCount] = useState(initialCount);

  return (
    <button onClick={() => setCount(c => c + 1)}>
      Count: {count}
    </button>
  );
}

API Route (App Router)

// app/api/users/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const users = await db.user.findMany();
  return NextResponse.json(users);
}

export async function POST(request: Request) {
  const body = await request.json();
  const user = await db.user.create({ data: body });
  return NextResponse.json(user, { status: 201 });
}

Essential Packages

PackagePurpose
typescriptType safety
tailwindcssStyling
@tanstack/react-queryServer state
zustandClient state
react-hook-formForms
zodValidation
framer-motionAnimations
lucide-reactIcons

TypeScript Rules

  1. Explicit prop types - Interface for every component
  2. No any - Use unknown if needed
  3. Strict mode - Enable in tsconfig
  4. Type imports - Use import type when possible

Code Quality Rules

  1. Functional components - No class components
  2. Named exports - Except page defaults
  3. Small files - One component per file
  4. Custom hooks - Extract reusable logic
  5. Error boundaries - Wrap key sections
  6. Loading states - Always handle loading/error

Source

git clone https://github.com/shahtuyakov/claude-setup/blob/main/skills/react-patterns/SKILL.mdView on GitHub

Overview

This skill covers patterns for building modern web apps with React and Next.js using TypeScript, App Router, and Server Components. It guides component design, data fetching, forms, state management, and client-side authentication, backed by best-practice rules and essential packages.

How This Skill Works

Patterns are organized around framework choice, reference files, and quick-start examples. It emphasizes TypeScript rules, code quality, and practical usage of packages such as @tanstack/react-query, zustand, react-hook-form, and zod to implement robust UI, data flow, and forms across server and client boundaries.

When to Use It

  • Build a Next.js App Router project with SEO emphasis and server components for pages.
  • Migrate a legacy project to Next.js Pages Router for a simpler mental model.
  • Create a client-only SPA (e.g., Vite + React) when SSR isn't required.
  • Implement forms with validation and client-side authentication using a pattern-driven approach.
  • Manage server data fetching and client state with libraries like react-query and zustand.

Quick Start

  1. Step 1: Initialize a Next.js + TypeScript project with App Router and install core packages (typescript, tailwindcss, @tanstack/react-query, zustand, react-hook-form, zod, framer-motion, lucide-react).
  2. Step 2: Create a server component page (app/users/page.tsx) that fetches data and composes a Client Component (UserList) for rendering.
  3. Step 3: Add an API route (app/api/users/route.ts) with GET/POST and implement a client-side form using react-hook-form and zod for validation.

Best Practices

  • Explicit prop types for every component using interfaces.
  • No any; use unknown when needed and prefer import type for type-only imports.
  • Enable strict mode in tsconfig to ensure type safety.
  • Small files with one component per file and named exports.
  • Error boundaries and loading states everywhere; extract reusable logic into custom hooks.

Example Use Cases

  • Next.js App Router page fetching users with getUsers and rendering a UserList.
  • A client component using useState (Counter) for interactive UI.
  • An App Router API route with GET and POST using NextResponse.
  • A server component pattern that composes data fetching with UI components.
  • A form built with react-hook-form and zod validation for robust input handling.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers