reactjs
  1. reactjs-flux-vs-mvc

ReactJS Flux Vs MVC

Heading h1

This page compares the concepts of ReactJS Flux and the traditional Model-View-Controller (MVC) architecture.

Syntax

In MVC, the application is divided into three components: models, views, and controllers. The controller acts as an intermediary between the model and view, and updates the view based on the changes made to the model.

Flux, on the other hand, introduces the concept of unidirectional data flow. In flux, instead of a single controller, we have actions, stores, and views. Actions are responsible for updating the state of the application and triggering updates. Stores hold the state of the application and notify views of changes. Views are responsible for rendering the UI based on the state of the stores.

Example

MVC

// Model
class Model {
  constructor() {
    this.value = 0;
  }

  increment() {
    this.value++;
  }
}

// View
class View {
  constructor(controller) {
    this.controller = controller;
    this.button = document.getElementById("increment");
    this.valueSpan = document.getElementById("value");
    this.button.addEventListener("click", this.controller.increment.bind(this.controller));
  }

  update(value) {
    this.valueSpan.innerText = value;
  }
}

// Controller
class Controller {
  constructor(model, view) {
    this.model = model;
    this.view = view;
  }

  increment() {
    this.model.increment();
    this.view.update(this.model.value);
  }
}

const model = new Model();
const view = new View(new Controller(model, view));

Flux

// Action
const increment = (dispatch) => {
  dispatch({ type: "INCREMENT" });
};

// Store
class Store {
  constructor(dispatcher) {
    this.value = 0;

    dispatcher.register((action) => {
      switch (action.type) {
        case "INCREMENT":
          this.value++;
          this.emitChange();
          break;
        default:
          break;
      }
    });
  }

  getValue() {
    return this.value;
  }

  emitChange() {
    this.listeners.forEach((listener) => listener());
  }

  addListener(listener) {
    this.listeners.push(listener);
  }

  removeListener(listener) {
    this.listeners = this.listeners.filter((l) => l !== listener);
  }
}

// View
class View {
  constructor(store) {
    this.store = store;
    this.valueSpan = document.getElementById("value");
  }

  update() {
    const value = this.store.getValue();
    this.valueSpan.innerText = value;
  }
}

const dispatcher = new Dispatcher();
const store = new Store(dispatcher);
const view = new View(store);

store.addListener(view.update.bind(view));

Output

The output in both the MVC and Flux examples would be a web page with a button and a number that increases by 1 each time the button is clicked.

Explanation

MVC has been a popular architecture for developing web applications for a long time. However, as web applications became more complex, MVC started to show some limitations. One of the biggest issues with MVC is that it can quickly become unwieldy when dealing with complex state management.

Flux was developed as an alternative to MVC, specifically keeping complex state management in mind. It introduced the concept of unidirectional data flow, which makes it easier to manage complex state and maintain consistency across the application.

Use

MVC is still a popular architecture and is used in many web frameworks like Ruby on Rails and Laravel. Flux, on the other hand, is still a relatively new concept and is used mostly in React applications.

Flux isn't necessarily the best choice for every situation, but it does work well in situations where there is a lot of complex state management involved.

Important Points

  • MVC is a traditional architecture for web applications that divides the application into models, views, and controllers.
  • Flux is an alternative to MVC that introduces the concept of unidirectional data flow and divides the application into actions, stores, and views.
  • Flux is useful for complex state management in React applications.
  • MVC is still a popular architecture and is used in many web frameworks.

Summary

In summary, MVC and Flux are two popular architectures for developing web applications. While MVC has been around for a long time and is still used in many web frameworks, Flux was developed more recently as an alternative to MVC, specifically keeping complex state management in mind. Both architectures have their advantages and disadvantages, and the choice between them ultimately depends on the needs of the application.

Published on: