How does a React frontend consume a microservices backend without coupling to every service?

Topic: microservices, React Side. Level: Intermediate.

Answer

The frontend should talk to **one aggregation layer** — an API Gateway or a BFF — not directly to dozens of services, so it isn't coupled to internal topology, CORS sprawl, or N round-trips. The BFF composes/trims data for the screen. In React, centralize the base URL and auth in one API client and use a data-fetching library (React Query/SWR) for caching, retries, and loading/error states. ```jsx // One client points at the gateway/BFF, not individual services const api = axios.create({ baseURL: import.meta.env.VITE_BFF_URL }) api.interceptors.request.use(c => { c.headers.Authorization = `Bearer ${getToken()}` return c }) // React Query handles caching/retries/loading so components stay simple function useHome() { return useQuery({ queryKey: ['home'], queryFn: () => api.get('/mobile/home').then(r => r.data) }) } ```