ποΈ MVVM (Model-View-ViewModel) Architecture β Explained for Frontend Developers
MVVM is a popular architectural pattern used to separate UI from business logic, making applications easier to build, test, and maintain.
It is commonly used in:
- Angular
- Vue.js
- Mobile frameworks like Android, SwiftUI, Flutter
- React projects (conceptually, although React doesn’t enforce MVVM)
π§ What is MVVM?
MVVM stands for:
M β Model
V β View
VM β ViewModel
Enter fullscreen mode Exit fullscreen mode
It separates responsibilities into three layers.
ποΈ Architecture Diagram
User
β
βΌ
βββββββββββ
β View β
β (UI) β
ββββββ¬βββββ
β User actions
βΌ
βββββββββββββββ
β ViewModel β
β Business β
β Logic β
ββββββ¬βββββββββ
β
βΌ
βββββββββββββββ
β Model β
β Data/API β
βββββββββββββββ
Enter fullscreen mode Exit fullscreen mode
1οΈβ£ Model
The Model is responsible for data.
It knows:
- API calls
- Database
- Business entities
- Validation
- Local storage
Example:
export async function fetchUsers() {
const res = await fetch("/api/users");
return res.json();
}
Enter fullscreen mode Exit fullscreen mode
The Model doesn’t know anything about the UI.
2οΈβ£ View
The View is what the user sees.
Example:
function UserList() {
return (
<div>
{/* Render users */}
</div>
);
}
Enter fullscreen mode Exit fullscreen mode
The View should:
β Display data
β Handle user interactions
β Not contain business logic
3οΈβ£ ViewModel
The ViewModel connects the View and Model.
Responsibilities:
- Calls APIs
- Stores UI state
- Formats data
- Handles loading
- Handles errors
- Exposes actions to the View
Example:
function useUsersViewModel() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(false);
async function loadUsers() {
setLoading(true);
const data = await fetchUsers();
setUsers(data);
setLoading(false);
}
return {
users,
loading,
loadUsers
};
}
Enter fullscreen mode Exit fullscreen mode
Notice:
The component doesn’t know how users are fetched.
βοΈ MVVM in React
React doesn’t officially implement MVVM.
But many teams structure code like this:
src/
β
βββ models/
β userApi.js
β
βββ viewmodels/
β useUsers.js
β
βββ views/
β UserList.jsx
β
βββ components/
Enter fullscreen mode Exit fullscreen mode
Here:
- Model β API services
- ViewModel β Custom hooks
- View β React components
Custom hooks often act as the ViewModel because they manage state and business logic.
π Real Example
Imagine an Employee Dashboard.
Model
Employee API
Enter fullscreen mode Exit fullscreen mode
β
ViewModel
Fetch employees
Sort employees
Handle loading
Handle errors
Enter fullscreen mode Exit fullscreen mode
β
View
Employee Table
Search Input
Loading Spinner
Enter fullscreen mode Exit fullscreen mode
The View simply renders the data it receives.
β Advantages
- Clear separation of concerns
- Easier unit testing
- Better code reuse
- Easier maintenance
- Scales well for large applications
β Disadvantages
- More files and abstraction
- Can feel excessive for small apps
- Requires consistent project structure
π MVVM vs MVC
MVC MVVM Controller handles user actions ViewModel handles presentation logic View often communicates with Controller View binds to ViewModel Common in backend frameworks Common in modern frontend and mobile appsπ¨ Interview Traps
β “React follows MVVM.”
Not by default.
React is a UI library, not an architectural framework.
However, you can organize a React application using MVVM principles.
β “ViewModel is the same as the Model.”
No.
- Model β Data layer
- ViewModel β Presentation/business logic
- View β UI
β “Business logic belongs in the View.”
No.
Business logic should live in the ViewModel (or equivalent layer), keeping components focused on rendering.
π‘ Senior-Level Insight
In large React applications, many teams naturally evolve toward an MVVM-like architecture:
- Model β API services, repositories
- ViewModel β Custom hooks, state management
- View β Presentational components
This keeps components small, reusable, and easy to test.
π― Interview One-Liner
MVVM is an architectural pattern that separates an application into Model (data), View (UI), and ViewModel (presentation logic), improving maintainability, testability, and scalability. In React, custom hooks often serve the role of the ViewModel.
π Quick Revision
Layer Responsibility React Example Model Data access, APIs, business entitiesuserApi.js, service layer
ViewModel
State, business logic, data transformation
useUsers() custom hook
View
Render UI and handle user interactions
UserList.jsx
λ΅κΈ λ¨κΈ°κΈ°