When building React applications, beginners often write everything inside a single component. While this may work for small projects, real-world applications require structured and maintainable architecture.
Professional React projects usually separate code into:
API Layer
Service Layer
Page Components
Reusable UI Components
This separation keeps the application organized, scalable, and easy to maintain.
In this article, we will understand the concept and examine the actual code implementation using an Employee Management example.
1. How a Structured React Application Works
A well-designed React application follows this flow:
Backend API
↓
Axios API Layer
↓
Service Layer
↓
Page Component (Logic & State)
↓
UI Components (Display)
This approach ensures that each layer has a clear responsibility.
2. Project Folder Structure
A clean project structure might look like this:
src
│
├── api
│ └── axiosClient.js
│
├── services
│ └── employeeService.js
│
├── components
│ ├── Header.jsx
│ ├── EmployeeForm.jsx
│ └── EmployeeTable.jsx
│
├── pages
│ └── EmployeePage.jsx
│
├── App.js
└── index.jsLet’s understand each part with code.
3. API Layer (Axios Configuration)
The API layer centralizes API configuration.
File:
src/api/axiosClient.js
import axios from "axios";
const axiosClient = axios.create({
baseURL: "http://localhost:5288/api",
headers: {
"Content-Type": "application/json"
}
});
export default axiosClient;Why this layer is useful
Instead of repeating API configuration everywhere, we create it once and reuse it across the application.
Later, this file can also be used for:
Adding JWT tokens
Error handling
Interceptors
4. Service Layer (Business API Calls)
The service layer communicates with the backend API.
File:
src/services/employeeService.jsimport axiosClient from "../api/axiosClient";
export const getEmployees = () => {
return axiosClient.get("/Employee");
};
export const addEmployee = (data) => {
return axiosClient.post("/Employee", data);
};
export const updateEmployee = (id, data) => {
return axiosClient.put(`/Employee/${id}`, data);
};
export const deleteEmployee = (id) => {
return axiosClient.delete(`/Employee/${id}`);
};Why this layer is important
The UI components do not call Axios directly. Instead, they call service functions.
This improves:
Code readability
Maintainability
Reusability
5. Header Component
The header is a simple UI component.
File:
src/components/Header.jsxfunction Header() {
return (
<div className="bg-blue-500 text-white p-6 rounded shadow text-3xl text-center mb-6">
Employee Management
</div>
);
}
export default Header;This component only handles UI display.
6. Employee Form Component
This component manages input fields and buttons.
File:
src/components/EmployeeForm.jsxfunction EmployeeForm({
name,
department,
setName,
setDepartment,
addEmployee,
updateEmployee
}) {
return (
<div className="flex gap-4 mb-6">
<input
placeholder="Name"
className="border px-4 py-2 rounded flex-1"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<input
placeholder="Department"
className="border px-4 py-2 rounded flex-1"
value={department}
onChange={(e) => setDepartment(e.target.value)}
/>
<button
onClick={addEmployee}
className="bg-green-500 text-white px-4 py-2 rounded"
>
Add
</button>
<button
onClick={updateEmployee}
className="bg-yellow-500 text-white px-4 py-2 rounded"
>
Update
</button>
</div>
);
}
export default EmployeeForm;
Join the conversation! Your thoughts help the community grow.