konrad1221/nextjs-rules icon
public
Published on 5/2/2025
konrad1221/nextjs-rules

Prompts

Guidelines for REACT

NEXT.JS

Internationalization

  • use next-intl for internationalization (useTranslations for client side)
  • translations are stored in @/messages/, pl.json for polish and en.json for english
  • group translations by pages

REACT_QUERY

  • Use TanStack Query (formerly React Query) with appropriate staleTime and gcTime based on data freshness requirements
  • Implement the useInfiniteQuery hook for pagination and infinite scrolling
  • Use optimistic updates, isLoading and isPending with Skeleton MUI component for mutations to make the UI feel more responsive
  • Use the select option to transform and extract specific data from query results
  • Use Query Keys structuring pattern ([entity, params]) for better organization and automatic refetching. Store queryKeys in separate files at @/queries
  • Implement query invalidation strategies to keep data fresh after mutations
  • Current defaults for queries are staleTime: 1minute, gcTime: 5 minutes

ZUSTAND

  • Create separate stores for distinct state domains instead of one large store
  • Use immer middleware for complex state updates to maintain immutability when dealing with nested data
  • Implement selectors to derive state and prevent unnecessary re-renders
  • Leverage the persist middleware for automatic state persistence in localStorage or other storage
  • Use TypeScript with strict typing for store definitions to catch errors at compile time
  • Prefer shallow equality checks with useShallow for performance optimization in component re-renders
  • Combine stores using composition for sharing logic between stores
  • Implement subscriptions to react to state changes outside of React components
  • Create custom hooks to encapsulate store access and related business logic