Skip to content

Instantly share code, notes, and snippets.

@samuelbalogh
Created March 1, 2025 09:34
Show Gist options
  • Save samuelbalogh/306769ee524f68ce427038f63418583d to your computer and use it in GitHub Desktop.
Save samuelbalogh/306769ee524f68ce427038f63418583d to your computer and use it in GitHub Desktop.
# modern react
## modern react best practices
### 1. project structure
- use a feature-based structure (e.g., features/, components/, hooks/, utils/)
- separate concerns: logic in hooks, UI in components
- colocate component-specific styles and tests within the component folder
### 2. state management
- use `useContext` + `useReducer` for global state instead of prop drilling
- prefer Zustand or Jotai over Redux for lightweight state
- Recoil is useful for atomic state updates
- keep UI state local whenever possible
### 3. performance optimizations
- use `React.memo` for memoizing components
- use `useCallback` to memoize event handlers
- use `useMemo` to memoize computed values
- avoid unnecessary re-renders by carefully managing dependencies in `useEffect`
- prefer lazy loading with `React.lazy` and `Suspense` for large components
- enable automatic batching in React 18
- use React Profiler and the Chrome DevTools for performance debugging
### 4. hooks best practices
- follow the rules of hooks (only call hooks at the top level, only in function components)
- extract reusable logic into custom hooks
- debounce expensive effects using `useDebounce`
- manage async calls using `useEffect` with cleanup
- prefer `useRef` over `useState` to store mutable values without re-rendering
### 5. forms and controlled components
- use `react-hook-form` for lightweight, performant forms
- prefer controlled inputs over uncontrolled for consistency
- leverage Yup/Zod for schema validation
- debounce form submissions using `useDebounce`
### 6. server-side communication
- use React Query or SWR for data fetching and caching
- prefer GraphQL (Apollo/Urql) over REST when appropriate
- implement optimistic updates for better UX
- use `Suspense` for data fetching in future-proof applications
- normalize API responses using Zod or custom transformers
### 7. styling best practices
- prefer Tailwind CSS for utility-based styling
- use CSS Modules or Styled Components for scoped styles
- avoid inline styles for dynamic styling to prevent unnecessary re-renders
- use theme providers for global theming
### 8. testing best practices
- use Jest + React Testing Library for unit and integration testing
- write end-to-end (E2E) tests with Playwright or Cypress
- mock network requests using MSW (Mock Service Worker)
- follow the AAA (Arrange, Act, Assert) pattern in tests
- prefer testing behavior over implementation details
### 9. accessibility (a11y)
- use `@testing-library/jest-dom` for accessibility assertions
- prefer semantic HTML elements (e.g., `<button>`, `<label>`, `<nav>`)
- ensure keyboard navigation support
- use aria attributes where necessary
- test with screen readers
### 10. error handling and logging
- use Error Boundaries for catching UI errors
- log errors with Sentry, LogRocket, or `console.error`
- display meaningful error messages to users
- handle API failures gracefully with fallback UI
### 11. routing best practices
- use React Router v6+
- prefer nested routes for complex layouts
- use lazy loading with dynamic imports for route-based code splitting
- manage auth-based routes with ProtectedRoute components
### 12. deployment and production optimization
- enable tree shaking and code splitting
- use Vite for faster builds over Webpack
- leverage SSR (Next.js) for SEO and initial load performance
- prefetch data using `useQuery.prefetch` or `getStaticProps`
- use CDNs for asset delivery
- optimize images using `next/image` or responsive formats
- serve gzip or brotli compressed assets
### 13. security best practices
- sanitize user input to prevent XSS attacks
- use helmet for securing headers
- never expose API keys in client-side code
- validate backend responses with Zod
- implement rate limiting and authentication guards
### 14. future-proofing with react 18+
- use concurrent rendering for better responsiveness
- leverage `useTransition` and `useDeferredValue` for smoother UI updates
- prefer `useId` over manually managing unique IDs
### 15. recommended tools & libraries
- state management: Zustand, Jotai, Recoil
- forms: React Hook Form + Yup/Zod
- fetching: React Query, SWR
- UI: Tailwind CSS, shadcn/ui, Material UI
- testing: Jest, React Testing Library, MSW, Playwright
- logging: Sentry, LogRocket
- deployment: Vercel, Netlify, Docker
this guide provides a comprehensive approach to writing modern, maintainable react applications with best practices in mind.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment