This document outlines the coding standards and best practices for the codespaces-react project.
- General Principles
- JavaScript/React
- File Structure
- Naming Conventions
- Component Guidelines
- Styling
- Testing
- Configurations
- Git Workflow
- Code Quality
- Performance
- Security
- Write clean, readable, and maintainable code
- Follow DRY (Don't Repeat Yourself) principle
- Use meaningful variable and function names
- Add comments for complex logic
- Keep functions small and focused on a single responsibility
- Use ES6+ features (arrow functions, destructuring, template literals, etc.)
- Prefer
constoverletwhen possible - Use functional components with hooks instead of class components
- Use PropTypes for type checking
- Avoid inline styles; use CSS modules or styled-components
- Use meaningful default props
src/
├── components/ # Reusable UI components
├── pages/ # Page components
├── hooks/ # Custom React hooks
├── utils/ # Utility functions
├── constants/ # Constants and configuration
├── styles/ # Global styles and CSS modules
├── assets/ # Images, fonts, etc.
├── __tests__/ # Test files
└── index.jsx # App entry point
- Files: PascalCase for components (e.g.,
UserProfile.jsx), camelCase for utilities (e.g.,apiHelpers.js) - Components: PascalCase (e.g.,
UserProfile) - Functions/Variables: camelCase (e.g.,
handleSubmit,userData) - Constants: UPPER_SNAKE_CASE (e.g.,
API_BASE_URL) - Hooks: Start with
use(e.g.,useAuth)
- Keep components small and focused
- Use destructuring for props
- Avoid deep nesting; break into smaller components
- Use custom hooks for shared logic
- Handle loading and error states appropriately
- Use CSS modules for component-scoped styles
- Follow BEM (Block Element Modifier) naming convention
- Prefer utility-first approach with Tailwind CSS for rapid development
- Maintain design system consistency with CSS custom properties
- Use CSS Grid and Flexbox for layouts
- Implement CSS custom properties (variables) for theming
- Leverage CSS-in-JS libraries like styled-components for dynamic styling
- Utilize modern selectors and pseudo-classes
- Mobile-first approach with media queries
- Use relative units (rem, em, %) over fixed pixels
- Implement fluid typography with clamp() function
- Test across multiple devices and screen sizes
- Minimize CSS bundle size with purging unused styles
- Use CSS containment for better rendering performance
- Optimize images and fonts for web
- Implement critical CSS for above-the-fold content
- Ensure sufficient color contrast ratios
- Use semantic HTML with appropriate ARIA attributes
- Support keyboard navigation and focus management
- Test with screen readers and accessibility tools
- Write tests for all components and utilities
- Use descriptive test names
- Test both success and error scenarios
- Aim for good test coverage (>80%)
- Use React Testing Library for component tests
- Use
vite.config.jsfor build and development settings - Configure aliases for clean imports:
@/components,@/utils - Set up environment variables with
VITE_prefix for client-side access - Enable source maps in development, disable in production
- Extend from
react-appandreact-app/jest - Add custom rules for React hooks and prop validation
- Configure import sorting and unused variable detection
- Use
.eslintrc.jsonfor project-wide settings
- Store sensitive data in
.envfiles (not committed to git) - Use
process.envfor server-side,import.meta.envfor client-side - Document required environment variables in README
- Provide
.env.examplefor team setup
start: Development serverbuild: Production buildtest: Run test suitelint: Code lintingformat: Code formatting with Prettier
- Use descriptive commit messages
- Create feature branches for new work
- Write clear pull request descriptions
- Review code before merging
- Keep commits atomic and focused
- Run ESLint before committing
- Fix all linting errors and warnings
- Use Prettier for code formatting
- Run tests before pushing changes
- Use React.memo for expensive components
- Optimize re-renders with useMemo and useCallback
- Lazy load components when appropriate
- Minimize bundle size
- Never commit sensitive data (API keys, passwords)
- Use environment variables for configuration
- Validate user input
- Follow OWASP guidelines for web security