Overview
Our frontend codebase is large and constantly growing, with multiple developers contributing to it. Establishing consistent rules across key areas like data fetching and state management will make the code easier to follow, refactor, and test. It will also help newcomers understand existing patterns and adopt them quickly.Data Fetching with React Query
Hook Organization
AlluseMutation
and useQuery
hooks should be grouped by domain/feature in a single location: features/lib/feature-hooks.ts
. Never call data fetching hooks directly from component bodies.
Benefits:
- Easier refactoring and testing
- Simplified mocking for tests
- Cleaner components focused on UI logic
- Reduced clutter in
.tsx
files
❌ Don’t do
✅ Do
Query Keys Management
Query keys should be unique identifiers for specific queries. Avoid using boolean values, empty strings, or inconsistent patterns. Best Practice: Group all query keys in one centralized location (inside the hooks file) for easy management and refactoring.- Easy key renaming and refactoring
- Consistent key structure across the app
- Better query specificity control
- Centralized key management
Refetch vs Query Invalidation
Prefer usinginvalidateQueries
over passing refetch
functions between components. This approach is more maintainable and easier to understand.
❌ Don’t do
✅ Do
Dialog State Management
Use a centralized store or context to manage all dialog states in one place. This eliminates the need to pass local state between different components and provides global access to dialog controls.Implementation Example
- Centralized dialog state management
- No prop drilling of dialog states
- Easy to open/close dialogs from anywhere in the app
- Consistent dialog behavior across the application
- Simplified component logic