src/
├── components/
│ ├── atoms/ # Basic building blocks (buttons, inputs, etc.)
│ ├── molecules/ # Combinations of atoms (forms, cards, etc.)
│ └── organisms/ # Complex UI sections (headers, sidebars, etc.)
├── pages/ # Route-level components
├── layouts/ # Page layout templates
├── hooks/ # Custom React hooks
├── store/ # State management (Redux/Zustand)
├── utils/ # Helper functions and utilities
├── lib/ # Third-party library configurations
├── models/ # TypeScript interfaces and types
└── core/ # Core application logic
-
Components
- Use PascalCase for component files:
Button.tsx,UserProfile.tsx - Each component should have its own directory with the same name
- Include related files (styles, tests, types) in the component directory
Button/ ├── Button.tsx ├── Button.test.tsx ├── Button.styles.ts └── index.ts - Use PascalCase for component files:
-
Utilities and Helpers
- Use camelCase for utility files:
formatDate.ts,validateInput.ts - Group related utilities in directories:
utils/date/,utils/validation/
- Use camelCase for utility files:
-
Constants and Types
- Use UPPER_SNAKE_CASE for constant files:
API_ENDPOINTS.ts - Use PascalCase for type files:
UserTypes.ts
- Use UPPER_SNAKE_CASE for constant files:
-
Component Size
- Keep components under 250 lines of code
- If exceeding, consider splitting into smaller components
- Extract complex logic into custom hooks
- Break down large components into smaller, focused pieces
-
When to Create Reusable Components
- Create an atom/molecule when:
- The component is used in 3+ places
- It represents a clear UI pattern
- It has clear, defined props and behavior
- Keep components in the page directory when:
- They're specific to one page/feature
- They contain page-specific business logic
- They're unlikely to be reused
- Create an atom/molecule when:
-
Component Organization
// Imports order import React from 'react'; import external libraries... import local components... import hooks... import utils... import types... import styles... // Component structure interface Props { // Props definition } export const Component: React.FC<Props> = ({ prop1, prop2 }) => { // Hooks // State // Effects // Helper functions // Render return (...) };
-
TypeScript
- Always use TypeScript for type safety
- Define interfaces for component props
- Use type inference where possible
- Avoid
anytype
-
State Management
- Use local state for component-specific state
- Use context for theme, auth, and global UI state
- Use store (Redux/Zustand) for complex application state
-
Performance
- Use React.memo() for expensive components
- Implement useMemo() for expensive calculations
- Use useCallback() for function props
- Lazy load routes and large components
-
CSS/Styling
- Use CSS-in-JS or CSS modules
- Follow BEM naming convention if using CSS classes
- Keep styles colocated with components
- Use theme variables for colors, spacing, etc.
-
Component Creation
- Use functional components with hooks
- Keep components focused and single-responsibility
- Use composition over inheritance
- Implement proper error boundaries
-
Props
- Use destructuring for props
- Provide default props where appropriate
- Document complex props with JSDoc
- Use proper prop types
-
Testing
- Write tests for critical business logic
- Test component behavior, not implementation
- Use meaningful test descriptions
- Follow AAA pattern (Arrange, Act, Assert)
-
Code Quality
- Use ESLint and Prettier
- Write meaningful commit messages
- Review code before committing
- Document complex logic
-
New Component
- Is it a basic UI element? →
atoms/ - Is it a combination of atoms? →
molecules/ - Is it a complex section? →
organisms/ - Is it page-specific? →
pages/ComponentName/
- Is it a basic UI element? →
-
New Hook
- Is it used across components? →
hooks/ - Is it component-specific? → Keep it in component file
- Is it used across components? →
-
New Utility
- Is it generic? →
utils/ - Is it feature-specific? → Feature directory
- Is it API-related? →
core/api/
- Is it generic? →
-
New Type
- Is it a model/entity? →
models/ - Is it component-specific? → Component directory
- Is it shared? →
types/
- Is it a model/entity? →
-
Component Documentation
- Document props using JSDoc
- Include usage examples
- Document side effects
- Explain complex logic
-
Code Comments
- Write why, not what
- Document complex algorithms
- Explain business logic
- Mark TODOs with ticket numbers
Remember: These conventions are guidelines, not strict rules. Use judgment and consistency in application.