MVVM (Model-View-ViewModel) Architecture
DEV Community

MVVM (Model-View-ViewModel) Architecture

๐Ÿ—๏ธ 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

It separates responsibilities into three layers.

Architecture Diagram

User
 โ”‚
 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  View   โ”‚
โ”‚  (UI)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜
     โ”‚ User actions
     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ViewModel  โ”‚
โ”‚  Business   โ”‚
โ”‚  Logic      โ”‚
โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
     โ”‚
     โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Model     โ”‚
โ”‚  Data/API   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

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();
}

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>
  );
}

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 };
}

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/

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
    โ†“
ViewModel
Fetch employees
Sort employees
Handle loading
Handle errors
    โ†“
View
Employee Table
Search Input
Loading Spinner

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 entities userApi.js, service layer
ViewModel State, business logic, data transformation useUsers() custom hook
View Render UI and handle user interactions UserList.jsx

#ReactJS #Frontend #MVVM #Architecture #JavaScript #SoftwareEngineering #InterviewPrep #EngineeringMindset

Comments

No comments yet. Start the discussion.