跪拜 Guibai
← All articles
Frontend · Backend

Stop Sprinkling Axios Calls Through Your React Components

By 东风破_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Frontend projects that skip an API layer accumulate duplicated Axios configuration and brittle endpoint strings across dozens of files. Centralizing that logic early costs almost nothing and prevents a painful refactor when the backend URL scheme, auth headers, or error handling inevitably change.

Summary

Dropping Axios calls directly into React components works for a demo but turns every page into a tangle of UI logic, endpoint URLs, and request configuration. Extracting all HTTP concerns into a dedicated `api/` directory with a single shared Axios instance and domain-specific modules like `user.js` gives pages a simple function call — `login(data)` — while the API layer owns the method, path, and serialization.

A Vite Mock plugin supplies a fake backend during development, intercepting requests such as `POST /api/login` and returning hardcoded JSON so the frontend can be built and tested without a real server. Mock and the API layer are separate concerns: one simulates the server, the other organizes how the client talks to it.

The result is a clear call stack: the page invokes an API function, the function delegates to the shared Axios instance, and the instance sends the HTTP request. This layering makes base URLs, timeouts, and interceptors changeable in one place and keeps components focused on user interaction.

Takeaways
A single shared Axios instance in `config.js` sets `baseURL`, timeout, and any interceptors once for the whole project.
Domain modules like `user.js` export named async functions (`login`, `getUserInfo`) that call the shared instance, so pages never import Axios directly.
Pages import only the API function they need and call it with plain data, keeping component code about user interaction, not HTTP details.
Vite's `vite-plugin-mock` intercepts requests during development, matching URL and method to return fake JSON without a running backend.
Mock files live in a separate `mock/` directory and simulate the server; the `api/` directory organizes the client's request code — two distinct layers.
Conclusions

Beginners often conflate Mock with the API layer, but Mock is a stand-in server while the API layer is client-side code organization; confusing them leads to tangled test data and production paths.

The article's structure — deferring JWT entirely to first establish a clean request architecture — mirrors how real projects should grow: solve the communication pattern before adding auth complexity.

Concepts & terms
API layer (frontend)
A dedicated set of modules in a frontend project that encapsulate all HTTP request logic — endpoint paths, methods, headers, and serialization — so UI components call simple functions instead of using Axios or fetch directly.
Mock server (Vite plugin)
A development-only tool that intercepts HTTP requests matching configured URL patterns and returns predefined JSON responses, letting frontend developers work without a real backend running.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗