redux vs mobx vs vuex
State Management Libraries für Webanwendungen
reduxmobxvuexÄhnliche Pakete:

State Management Libraries für Webanwendungen

State Management Libraries sind essentielle Werkzeuge in der Webentwicklung, die Entwicklern helfen, den Zustand einer Anwendung effizient zu verwalten. Diese Bibliotheken bieten strukturierte Ansätze zur Handhabung von Anwendungsdaten, um sicherzustellen, dass die Benutzeroberfläche immer mit dem aktuellen Zustand synchronisiert ist. MobX, Redux und Vuex sind drei beliebte Optionen, die jeweils unterschiedliche Philosophien und Architekturen zur Verwaltung des Anwendungszustands bieten.

npm-Download-Trend

3 Jahre

GitHub Stars Ranking

Statistik-Detail

Paket
Downloads
Stars
Größe
Issues
Veröffentlichung
Lizenz
redux62,844,40661,441290 kB17vor 3 JahrenMIT
mobx4,632,75028,2194.89 MB19vor 3 TagenMIT
vuex2,445,56528,289271 kB144-MIT

Funktionsvergleich: redux vs mobx vs vuex

Architektur

  • redux:

    Redux folgt einem unidirektionalen Datenfluss und verwendet ein zentrales Store-Konzept, in dem der gesamte Anwendungszustand gespeichert wird. Aktionen werden verwendet, um Zustandsänderungen auszulösen, und Reducer sind Funktionen, die den neuen Zustand basierend auf diesen Aktionen berechnen. Dies fördert eine klare Trennung von Anliegen und macht den Zustand vorhersehbar.

  • mobx:

    MobX verwendet ein reaktives Programmiermodell, das es ermöglicht, den Zustand der Anwendung automatisch zu verfolgen und zu aktualisieren. Es basiert auf der Idee von 'observable' und 'reaction', was bedeutet, dass Änderungen im Zustand automatisch in der Benutzeroberfläche reflektiert werden, ohne dass zusätzliche Logik erforderlich ist.

  • vuex:

    Vuex ist eine zentrale Zustandsverwaltung für Vue.js-Anwendungen. Es nutzt ein zentrales Store-Objekt, das den Zustand der Anwendung enthält, und ermöglicht es Komponenten, auf diesen Zustand zuzugreifen und ihn zu ändern. Vuex integriert sich nahtlos in die Vue-Entwicklungsumgebung und bietet eine strukturierte Möglichkeit, den Zustand zu verwalten.

Reaktivität

  • redux:

    Redux verwendet eine explizite Zustandsverwaltung, die nicht reaktiv ist. Änderungen am Zustand müssen durch Aktionen und Reducer ausgelöst werden, was bedeutet, dass die Entwickler manuell steuern müssen, wann die Benutzeroberfläche aktualisiert wird. Dies kann zu einer weniger intuitiven Entwicklung führen, bietet jedoch eine klare Nachvollziehbarkeit der Zustandsänderungen.

  • mobx:

    MobX bietet eine reaktive Programmierung, bei der der Zustand automatisch aktualisiert wird, wenn sich abhängige Daten ändern. Entwickler können 'observable' Objekte erstellen, und die Benutzeroberfläche wird automatisch aktualisiert, wenn sich diese Objekte ändern, was die Entwicklung intuitiver macht.

  • vuex:

    Vuex nutzt die reaktive Natur von Vue.js, um den Zustand der Anwendung zu verwalten. Änderungen im Store führen automatisch zu Aktualisierungen in den Komponenten, die diesen Zustand verwenden. Dies ermöglicht eine reibungslose Synchronisierung zwischen Zustand und Benutzeroberfläche.

Lernkurve

  • redux:

    Redux hat eine steilere Lernkurve, da es ein komplexeres Konzept mit Middleware, Aktionen und Reducern einführt. Entwickler müssen die Prinzipien des unidirektionalen Datenflusses und der funktionalen Programmierung verstehen, was anfangs herausfordernd sein kann.

  • mobx:

    MobX hat eine flachere Lernkurve im Vergleich zu Redux, da es weniger Boilerplate-Code erfordert und eine intuitive API bietet. Entwickler können schnell mit der Implementierung beginnen, insbesondere wenn sie bereits mit reaktiver Programmierung vertraut sind.

  • vuex:

    Vuex hat eine moderate Lernkurve, insbesondere für Entwickler, die bereits mit Vue.js vertraut sind. Es bietet eine strukturierte und klare API, die es einfach macht, den Zustand zu verwalten, aber es erfordert dennoch ein gewisses Verständnis der zugrunde liegenden Konzepte.

Erweiterbarkeit

  • redux:

    Redux ist sehr erweiterbar und hat ein starkes Ökosystem von Middleware und Tools, die die Funktionalität erweitern können. Entwickler können benutzerdefinierte Middleware erstellen, um zusätzliche Logik in den Datenfluss einzufügen, und es gibt viele Bibliotheken, die die Integration mit anderen Technologien erleichtern.

  • mobx:

    MobX ist sehr flexibel und kann leicht in bestehende Projekte integriert werden. Es bietet eine einfache API, die es Entwicklern ermöglicht, ihre eigenen Lösungen zu implementieren, ohne sich an strikte Konventionen halten zu müssen.

  • vuex:

    Vuex bietet eine strukturierte Möglichkeit zur Erweiterung, indem es Module unterstützt, die es ermöglichen, den Zustand in kleinere, handhabbare Teile zu unterteilen. Dies erleichtert die Verwaltung komplexer Anwendungen und fördert die Wiederverwendbarkeit von Code.

Community und Unterstützung

  • redux:

    Redux hat eine der größten und aktivsten Communities im Bereich der Zustandsverwaltung. Es gibt eine Fülle von Ressourcen, Tutorials und Middleware, die Entwicklern helfen, ihre Anwendungen zu optimieren und zu erweitern.

  • mobx:

    MobX hat eine wachsende Community und bietet eine Vielzahl von Ressourcen, einschließlich Dokumentation und Tutorials. Es ist jedoch nicht so weit verbreitet wie Redux, was die Verfügbarkeit von Drittanbieter-Plugins und -Tools betrifft.

  • vuex:

    Vuex hat eine engagierte Community, die sich auf die Vue.js-Entwicklung konzentriert. Es gibt viele Ressourcen und Dokumentationen, die speziell auf die Integration von Vuex in Vue-Anwendungen abzielen.

Wie man wählt: redux vs mobx vs vuex

  • redux:

    Wählen Sie Redux, wenn Sie eine vorhersehbare Zustandsverwaltung mit einem klaren Fluss von Daten und Aktionen benötigen. Redux ist besonders nützlich für große Anwendungen mit komplexen Zustandsanforderungen und bietet eine starke Community und viele Middleware-Optionen.

  • mobx:

    Wählen Sie MobX, wenn Sie eine reaktive Programmierweise bevorzugen und eine einfache Integration in bestehende Projekte wünschen. MobX eignet sich hervorragend für kleinere bis mittelgroße Anwendungen, wo die Flexibilität und die einfache Handhabung von Zustandsänderungen wichtig sind.

  • vuex:

    Wählen Sie Vuex, wenn Sie eine Vue.js-Anwendung entwickeln und eine integrierte Lösung für die Zustandsverwaltung benötigen. Vuex ist speziell für Vue.js konzipiert und bietet eine klare Struktur und einfache Integration in Vue-Komponenten.

README für redux

Redux Logo

Redux is a predictable state container for JavaScript apps.

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

You can use Redux together with React, or with any other view library. The Redux core is tiny (2kB, including dependencies), and has a rich ecosystem of addons.

Redux Toolkit is our official recommended approach for writing Redux logic. It wraps around the Redux core, and contains packages and functions that we think are essential for building a Redux app. Redux Toolkit builds in our suggested best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.

GitHub Workflow Status npm version npm downloads redux channel on discord

Installation

Create a React Redux App

The recommended way to start new apps with React and Redux Toolkit is by using our official Redux Toolkit + TS template for Vite, or by creating a new Next.js project using Next's with-redux template.

Both of these already have Redux Toolkit and React-Redux configured appropriately for that build tool, and come with a small example app that demonstrates how to use several of Redux Toolkit's features.

# Vite with our Redux+TS template
# (using the `degit` tool to clone and extract the template)
npx degit reduxjs/redux-templates/packages/vite-template-redux my-app

# Next.js using the `with-redux` template
npx create-next-app --example with-redux my-app

We do not currently have official React Native templates, but recommend these templates for standard React Native and for Expo:

npm install @reduxjs/toolkit react-redux

For the Redux core library by itself:

npm install redux

For more details, see the Installation docs page.

Documentation

The Redux core docs are located at https://redux.js.org, and include the full Redux tutorials, as well usage guides on general Redux patterns:

The Redux Toolkit docs are available at https://redux-toolkit.js.org, including API references and usage guides for all of the APIs included in Redux Toolkit.

Learn Redux

Redux Essentials Tutorial

The Redux Essentials tutorial is a "top-down" tutorial that teaches "how to use Redux the right way", using our latest recommended APIs and best practices. We recommend starting there.

Redux Fundamentals Tutorial

The Redux Fundamentals tutorial is a "bottom-up" tutorial that teaches "how Redux works" from first principles and without any abstractions, and why standard Redux usage patterns exist.

Help and Discussion

The #redux channel of the Reactiflux Discord community is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - please come and join us there!

Before Proceeding Further

Redux is a valuable tool for organizing your state, but you should also consider whether it's appropriate for your situation. Please don't use Redux just because someone said you should - instead, please take some time to understand the potential benefits and tradeoffs of using it.

Here are some suggestions on when it makes sense to use Redux:

  • You have reasonable amounts of data changing over time
  • You need a single source of truth for your state
  • You find that keeping all your state in a top-level component is no longer sufficient

Yes, these guidelines are subjective and vague, but this is for a good reason. The point at which you should integrate Redux into your application is different for every user and different for every application.

For more thoughts on how Redux is meant to be used, please see:

Basic Example

The whole global state of your app is stored in an object tree inside a single store. The only way to change the state tree is to create an action, an object describing what happened, and dispatch it to the store. To specify how state gets updated in response to an action, you write pure reducer functions that calculate a new state based on the old state and the action.

Redux Toolkit simplifies the process of writing Redux logic and setting up the store. With Redux Toolkit, the basic app logic looks like:

import { createSlice, configureStore } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    incremented: state => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1
    },
    decremented: state => {
      state.value -= 1
    }
  }
})

export const { incremented, decremented } = counterSlice.actions

const store = configureStore({
  reducer: counterSlice.reducer
})

// Can still subscribe to the store
store.subscribe(() => console.log(store.getState()))

// Still pass action objects to `dispatch`, but they're created for us
store.dispatch(incremented())
// {value: 1}
store.dispatch(incremented())
// {value: 2}
store.dispatch(decremented())
// {value: 1}

Redux Toolkit allows us to write shorter logic that's easier to read, while still following the original core Redux behavior and data flow.

Logo

You can find the official logo on GitHub.

Change Log

This project adheres to Semantic Versioning. Every release, along with the migration instructions, is documented on the GitHub Releases page.

License

MIT