mobx vs redux vs vuex
狀態管理庫
mobxreduxvuex類似パッケージ:

狀態管理庫

狀態管理庫是用於管理應用程序中狀態的工具,特別是在大型應用中,狀態的管理變得越來越複雜。這些庫提供了一種結構化的方法來管理應用程序的狀態,確保數據在不同組件之間的一致性和可預測性。選擇合適的狀態管理庫可以提高開發效率,減少錯誤,並改善應用程序的可維護性。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
mobx028,2194.89 MB193日前MIT
redux061,441290 kB173年前MIT
vuex028,289271 kB144-MIT

機能比較: mobx vs redux vs vuex

設計原則

  • mobx:

    MobX 採用反應式編程的設計原則,允許開發者以更自然的方式管理狀態。它通過觀察者模式自動更新 UI,減少了手動管理狀態的需求,並且使得代碼更簡潔易懂。

  • redux:

    Redux 的設計原則強調單一狀態樹和不可變數據,這使得狀態變更變得可預測且易於追蹤。這種設計使得應用的狀態管理變得透明,並且便於進行時間旅行調試。

  • vuex:

    Vuex 的設計原則是基於 Vue 的反應性系統,提供了一個集中式的狀態管理方案,並且支持模塊化。這使得大型應用的狀態管理更加清晰,並且能夠與 Vue 組件無縫集成。

學習曲線

  • mobx:

    MobX 的學習曲線相對較平緩,因為它的 API 簡單且直觀。開發者可以快速上手,並且在小型項目中實現快速開發。

  • redux:

    Redux 的學習曲線較陡,因為它需要理解中間件、不可變數據和 Redux 的工作流程。對於新手來說,掌握 Redux 可能需要更多的時間和實踐。

  • vuex:

    Vuex 的學習曲線與 Vue.js 相似,對於已經熟悉 Vue 的開發者來說,學習 Vuex 會相對容易。它的 API 設計與 Vue 的反應性系統緊密相連,使得學習過程更加自然。

狀態變更

  • mobx:

    MobX 允許直接修改狀態,並且自動追蹤依賴,這使得狀態變更更加靈活和高效。開發者可以使用可觀察的數據結構,當數據變更時,相關的 UI 自動更新。

  • redux:

    Redux 強調不可變數據,狀態變更必須通過發送動作來進行,這使得狀態的變更過程更加可預測和可追蹤。這種方式雖然增加了代碼的冗長性,但卻提高了應用的可維護性。

  • vuex:

    Vuex 也使用不可變數據的概念,狀態變更必須通過提交突變來進行。這使得狀態變更的過程更加透明,並且可以輕鬆地進行調試和追蹤。

性能

  • mobx:

    MobX 通過細粒度的反應性來提高性能,只有當依賴的狀態發生變化時,相關的組件才會重新渲染。這使得 MobX 在處理大量狀態時表現出色。

  • redux:

    Redux 的性能取決於應用的結構和狀態的大小。雖然 Redux 本身是高效的,但不當的使用可能導致不必要的重新渲染。使用選擇器和記憶化可以有效提高性能。

  • vuex:

    Vuex 的性能通常與 Vue 的性能相似,因為它利用了 Vue 的反應性系統。對於大型應用,合理的模塊化和狀態管理可以顯著提高性能。

擴展性

  • mobx:

    MobX 提供了靈活的擴展性,開發者可以根據需要定制和擴展其功能。由於其簡單的 API,MobX 可以輕鬆地與其他庫和框架集成。

  • redux:

    Redux 具有很高的擴展性,通過中間件和插件,開發者可以輕鬆地擴展 Redux 的功能。這使得 Redux 成為大型應用的理想選擇,因為它能夠適應不斷變化的需求。

  • vuex:

    Vuex 的擴展性體現在其模塊化的設計上,開發者可以根據應用的需求創建不同的模塊,並且可以輕鬆地與 Vue 生態系統中的其他工具集成。

選び方: mobx vs redux vs vuex

  • mobx:

    選擇 MobX 如果你需要一個簡單且直觀的狀態管理解決方案,特別是在需要快速開發和高效反應的應用中。MobX 的反應式編程模型使得狀態變更自動更新視圖,適合小型到中型應用。

  • redux:

    選擇 Redux 如果你需要一個可預測的狀態管理解決方案,並且希望在大型應用中保持狀態的可追蹤性和可維護性。Redux 的單一狀態樹和不可變數據結構使得狀態變更可預測,適合複雜的應用。

  • vuex:

    選擇 Vuex 如果你正在使用 Vue.js 框架,並且需要一個與 Vue 生態系統緊密集成的狀態管理解決方案。Vuex 提供了與 Vue 組件的良好協作,並且支持模塊化的狀態管理。

mobx のREADME

logo

MobX

Simple, scalable state management.

npm version OpenCollective OpenCollective Discuss on Github Coverage Status View changelog


Documentation

Documentation can be found at mobx.js.org.


Sponsors

MobX is made possible by the generosity of the sponsors below, and many other individual backers. Sponsoring directly impacts the longevity of this project.

🥇🥇 Platinum sponsors ($5000+ total contribution): 🥇🥇


Guilded Canva Parallax

🥇 Gold sponsors ($2500+ total contribution):


One Beyond Frontend Masters Auction Frontier CodeFirst Modulz Coinbase Curology Mendix Facebook Open Source Casino Sites Bugsnag

🥈 Silver sponsors ($500+ total contributions):

mantro GmbH Extremely Heavy Industries Algolia Space307 Blokt UPPER DAZN talentplot EaseUS Route Planner and Route Optimizer Handsontable

Introduction

Anything that can be derived from the application state, should be. Automatically.

MobX is a signal based, battle-tested library that makes state management simple and scalable by transparently applying functional reactive programming. The philosophy behind MobX is simple:

😙

Straightforward

Write minimalistic, boilerplate-free code that captures your intent. Trying to update a record field? Simply use a normal JavaScript assignment — the reactivity system will detect all your changes and propagate them out to where they are being used. No special tools are required when updating data in an asynchronous process.

🚅

Effortless optimal rendering

All changes to and uses of your data are tracked at runtime, building a dependency tree that captures all relations between state and output. This guarantees that computations that depend on your state, like React components, run only when strictly needed. There is no need to manually optimize components with error-prone and sub-optimal techniques like memoization and selectors.

🤹🏻‍♂️

Architectural freedom

MobX is unopinionated and allows you to manage your application state outside of any UI framework. This makes your code decoupled, portable, and above all, easily testable.


A quick example

So what does code that uses MobX look like?

import React from "react"
import ReactDOM from "react-dom"
import { makeAutoObservable } from "mobx"
import { observer } from "mobx-react-lite"

// Model the application state.
const myTimer = makeAutoObservable({
    secondsPassed: 0,
    increase() {
        this.secondsPassed += 1
    },
    reset() {
        this.secondsPassed = 0
    }
})

// Build a "user interface" that uses the observable state.
const TimerView = observer(({ timer }) => (
    <button onClick={() => timer.reset()}>Seconds passed: {timer.secondsPassed}</button>
))

ReactDOM.render(<TimerView timer={myTimer} />, document.body)

// Update the 'Seconds passed: X' text every second.
setInterval(() => {
    myTimer.increase()
}, 1000)

The observer wrapper around the TimerView React component will automatically detect that rendering depends on the timer.secondsPassed observable, even though this relationship is not explicitly defined. The reactivity system will take care of re-rendering the component when precisely that field is updated in the future.

Every event (onClick / setInterval) invokes an action (myTimer.increase / myTimer.reset) that updates observable state (myTimer.secondsPassed). Changes in the observable state are propagated precisely to all computations and side effects (TimerView) that depend on the changes being made.

MobX unidirectional flow

This conceptual picture can be applied to the above example, or any other application using MobX.

Getting started

To learn about the core concepts of MobX using a larger example, check out The gist of MobX page, or take the 10 minute interactive introduction to MobX and React. The philosophy and benefits of the mental model provided by MobX are also described in great detail in the blog posts UI as an afterthought and How to decouple state and UI (a.k.a. you don’t need componentWillMount).

Further resources

The MobX book

The MobX Quick Start Guide ($24.99) by Pavan Podila and Michel Weststrate is available as an ebook, paperback, and on the O'Reilly platform (see preview).

Videos

Credits

MobX is inspired by reactive programming principles, which are for example used in spreadsheets. It is inspired by model–view–viewmodel frameworks like MeteorJS's Tracker, Knockout and Vue.js, but MobX brings transparent functional reactive programming (TFRP, a concept which is further explained in the MobX book) to the next level and provides a standalone implementation. It implements TFRP in a glitch-free, synchronous, predictable and efficient manner.

A ton of credit goes to Mendix, for providing the flexibility and support to maintain MobX and the chance to prove the philosophy of MobX in a real, complex, performance critical applications.