Getting consistent, high-quality code from AI assistants in React and TypeScript projects requires clear instructions. Without them, you'll get a mix of class components and functional components, JavaScript and TypeScript, and inconsistent patterns.
This guide provides practical instructions you can use to get reliable AI-generated React and TypeScript code.
Why React/TypeScript Projects Need Special Attention
React and TypeScript have many "correct" ways to do things:
- Class components vs functional components
- useState vs useReducer vs external state
- CSS modules vs styled-components vs Tailwind
- Default exports vs named exports
- Implicit vs explicit return types
Without instructions, AI assistants will mix approaches based on their training data. Your codebase ends up inconsistent.
Essential Instructions for React Projects
Component Structure
## React Components
- Always use functional components with hooks
- Never use class components
- Use named exports, not default exports
- One component per file
- Component file names should be PascalCase (e.g., UserProfile.tsx)
Example structure:
export function UserProfile({ user }: UserProfileProps) {
return <div>...</div>;
}
Hooks Usage
## React Hooks
- Prefer useState for simple local state
- Use useReducer for complex state logic
- Extract custom hooks for reusable logic
- Custom hooks must start with "use" prefix
- Always include dependency arrays in useEffect/useMemo/useCallback
- Never ignore exhaustive-deps warnings
Example custom hook:
export function useUser(userId: string) {
const [user, setUser] = useState<User | null>(null);
// ...
return { user, isLoading, error };
}
Props and Types
## Props
- Define props interface above the component
- Name props interfaces as ComponentNameProps
- Destructure props in function signature
- Use children prop type from React.ReactNode
Example:
interface ButtonProps {
variant: 'primary' | 'secondary';
onClick: () => void;
children: React.ReactNode;
}
export function Button({ variant, onClick, children }: ButtonProps) {
// ...
}
State Management
## State Management
- Use React Context for global UI state (theme, auth)
- Use TanStack Query (React Query) for server state
- Keep state as close to usage as possible
- Avoid prop drilling beyond 2 levels—use context or composition
Styling
## Styling
- Use Tailwind CSS for all styling
- No inline styles except for dynamic values
- Use cn() utility for conditional classes
- Follow mobile-first responsive design
Example:
<button className={cn(
"px-4 py-2 rounded-md font-medium",
variant === "primary" && "bg-blue-500 text-white",
variant === "secondary" && "bg-gray-200 text-gray-800"
)}>
{children}
</button>
Essential Instructions for TypeScript
Type Definitions
## TypeScript Types
- Always use explicit types for function parameters
- Always use explicit return types for functions
- Prefer interfaces for object shapes
- Use type for unions, intersections, and primitives
- No `any` type—use `unknown` if type is truly unknown
- Enable strict mode
Example:
interface User {
id: string;
name: string;
email: string;
}
function getUser(id: string): Promise<User> {
// ...
}
Null Handling
## Null and Undefined
- Use optional chaining (?.) for potentially null values
- Use nullish coalescing (??) instead of || for defaults
- Prefer undefined over null for optional values
- Always handle null cases explicitly
Example:
const userName = user?.name ?? 'Anonymous';
Generics
## Generics
- Use generics for reusable type-safe utilities
- Name generic parameters descriptively (T is ok for simple cases)
- Constrain generics when possible
Example:
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
Enums and Constants
## Enums and Constants
- Prefer const objects over enums for better tree-shaking
- Use as const for literal types
Example:
const Status = {
Pending: 'pending',
Active: 'active',
Completed: 'completed',
} as const;
type Status = typeof Status[keyof typeof Status];
Complete React/TypeScript Instruction Template
Here's a comprehensive template you can adapt:
# React/TypeScript Coding Standards
## General
- Use TypeScript for all files (.tsx for components, .ts for utilities)
- Enable strict mode in tsconfig.json
- No console.log in production code (use proper logging)
- No commented-out code in commits
## React Components
- Functional components only, no class components
- Named exports, not default exports
- One component per file, file name matches component name
- Props interface defined above component as ComponentNameProps
- Destructure props in function signature
## Hooks
- Follow Rules of Hooks strictly
- Custom hooks start with "use" prefix
- Always specify dependency arrays
- Use useMemo/useCallback only when necessary (measure first)
## TypeScript
- Explicit types for all function parameters and returns
- Interfaces for object shapes, types for unions
- No `any`—use `unknown` or proper types
- Enable strict null checks
## Styling
- Tailwind CSS for all styles
- Mobile-first responsive design
- Use cn() for conditional classes
## Testing
- Test file next to source: Component.tsx → Component.test.tsx
- Use React Testing Library
- Test behavior, not implementation
- Minimum 80% coverage for new code
## File Structure
src/
├── components/ # Reusable UI components
├── features/ # Feature-specific components
├── hooks/ # Custom hooks
├── lib/ # Utilities and helpers
├── types/ # Shared TypeScript types
└── services/ # API and external services
The Problem with Static Instruction Files
You might put these instructions in an AGENTS.md or CLAUDE.md file. But here's what happens in practice:
- The file loads into every request—even when you're asking a simple question
- Token costs add up—a comprehensive instruction file consumes 10,000+ tokens per request
- The AI often ignores them—instructions get buried in context and aren't prioritized
You're paying for tokens that don't reliably work.
A Better Approach: SuperClawd
SuperClawd solves this with on-demand instruction loading:
- Create a React Standards skill with your component guidelines
- Create a TypeScript Standards skill with your type guidelines
- Create a Testing Standards skill with your testing patterns
When you're writing a React component, only the React skill loads. When you're writing types, only the TypeScript skill loads. No bloat, no buried instructions.
And because SuperClawd actively delivers instructions (not passively hoping they're noticed), you get 99.9% adherence to your standards.
Example: React Skill in SuperClawd
Skill Name: React Standards Action Type: Code Execution Mode: Auto
Categories:
-
Component Structure
- Use functional components with hooks
- Never use class components
- Use named exports, not default exports
- One component per file
-
Props and Types
- Define props interface as ComponentNameProps
- Destructure props in function signature
- Use React.ReactNode for children
-
Hooks
- Always include dependency arrays
- Custom hooks must start with "use"
- Use useMemo/useCallback only when necessary
-
Styling
- Use Tailwind CSS for all styling
- Use cn() utility for conditional classes
- Mobile-first responsive design
Quick Start Instructions
If you just want something to copy-paste right now, here are minimal instructions that cover the most common issues:
## React/TypeScript Quick Rules
1. Functional components only (no classes)
2. Named exports (no default exports)
3. Explicit TypeScript types for all parameters and returns
4. No `any` type
5. Tailwind for styling
6. Props interface as ComponentNameProps above component
7. Destructure props in function signature
This won't cover everything, but it addresses the most frequent inconsistencies.
Conclusion
Consistent AI-generated React and TypeScript code requires clear instructions covering:
- Component patterns (functional, named exports)
- TypeScript practices (explicit types, no any)
- Hooks usage (dependency arrays, custom hook naming)
- Styling approach (Tailwind, cn utility)
You can put these in static files, but you'll face token bloat and unreliable adherence. For production teams, SuperClawd provides on-demand loading and 99.9% reliability.
The goal isn't just to write instructions—it's to get the AI to actually follow them.
Want reliable React/TypeScript instructions? Try SuperClawd free and create your first skill in minutes.
Pro tip: Use coupon code WELCOME in your billing settings to get free credits when you sign up!