redux vs mobx vs vuex
状態管理ライブラリ
reduxmobxvuex類似パッケージ:
状態管理ライブラリ

状態管理ライブラリは、アプリケーションの状態を管理し、コンポーネント間でのデータの共有を容易にするためのツールです。これらのライブラリは、特に大規模なアプリケーションや複雑な状態を持つアプリケーションで、状態の一貫性を保ち、データフローを管理するのに役立ちます。状態管理ライブラリを使用することで、開発者はアプリケーションの状態をより簡単に追跡し、デバッグし、保守することができます。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
redux18,841,17661,422290 kB482年前MIT
mobx2,403,75628,1214.35 MB783ヶ月前MIT
vuex1,609,77928,420271 kB143-MIT
機能比較: redux vs mobx vs vuex

データバインディング

  • redux:

    Reduxでは、状態の変更はアクションを通じて行われ、状態はストアに格納されます。データバインディングは一方向で、状態の変更はUIに反映されますが、UIからの変更はアクションを通じて行う必要があります。

  • mobx:

    MobXは、リアクティブなデータバインディングを提供します。状態が変更されると、自動的に関連するUIが再描画されます。これにより、開発者は状態の変更を意識せずに、UIの更新を行うことができます。

  • vuex:

    Vuexは、Vue.jsのために設計された状態管理ライブラリで、Vueのリアクティブシステムと統合されています。Vuexを使用すると、状態の変更が自動的にコンポーネントに反映され、データの流れが明確になります。

学習曲線

  • redux:

    Reduxは、アクション、リデューサー、ストアといった概念を理解する必要があるため、学習曲線はやや急です。しかし、一度理解すれば、状態管理のフローが明確になり、アプリケーションのデバッグが容易になります。

  • mobx:

    MobXは、シンプルなAPIと直感的なリアクティブプログラミングモデルを提供しているため、比較的学習しやすいです。特に、状態管理の概念に慣れていない開発者にとっては、理解しやすい選択肢です。

  • vuex:

    Vuexは、Vue.jsに特化した状態管理ライブラリであり、Vueのコンポーネントと密接に連携しています。Vueの基本を理解している開発者にとっては、比較的簡単に学ぶことができます。

ミューテーションの管理

  • redux:

    Reduxでは、状態の変更はアクションを通じて行われ、すべての状態変更が明確に記録されます。これにより、状態の変更を追跡しやすく、デバッグが容易になります。

  • mobx:

    MobXでは、状態の変更は直接的に行われ、開発者は状態を自由に変更できます。これにより、柔軟性が高まりますが、状態の変更がどこで行われているかを追跡するのが難しくなることがあります。

  • vuex:

    Vuexでは、状態の変更はミューテーションを通じて行われ、すべての変更が明確に管理されます。これにより、状態の変更がどのように行われたかを追跡しやすくなります。

拡張性

  • redux:

    Reduxは、ミドルウェアを使用して機能を拡張することができ、サードパーティのライブラリと統合するのが容易です。また、Redux Toolkitを使用することで、開発者はより簡単にReduxを利用できます。

  • mobx:

    MobXは、シンプルなAPIと柔軟な設計により、プロジェクトのニーズに応じて容易に拡張できます。特に、リアクティブプログラミングの概念を活用することで、複雑な状態管理を簡素化できます。

  • vuex:

    Vuexは、Vue.jsのエコシステムと密接に統合されているため、Vueコンポーネントやプラグインと簡単に連携できます。これにより、アプリケーションのニーズに応じて拡張が容易です。

デバッグツール

  • redux:

    Reduxは、Redux DevToolsを使用することで、アクションや状態の変更を視覚化し、デバッグが容易になります。これにより、開発者はアプリケーションの状態を簡単に追跡できます。

  • mobx:

    MobXは、開発者ツールを使用して状態の変更を追跡することができますが、Reduxほど強力ではありません。MobXの状態管理はリアクティブであるため、状態の変化を視覚化するのが難しいことがあります。

  • vuex:

    Vuexは、Vue DevToolsと統合されており、状態の変更を視覚化することができます。これにより、開発者は状態の流れを理解しやすくなります。

選び方: redux vs mobx vs vuex
  • redux:

    Reduxを選ぶべき場合は、状態の予測可能性とデバッグの容易さを重視する場合です。Reduxは、状態を一元管理し、アクションを通じて状態を変更するため、アプリケーションのフローを明確に理解できます。

  • mobx:

    MobXを選ぶべき場合は、シンプルで直感的な状態管理を求めている場合です。MobXは、リアクティブプログラミングの原則に基づいており、状態の変更を自動的にUIに反映させることができます。

  • vuex:

    Vuexを選ぶべき場合は、Vue.jsアプリケーションの状態管理を統合的に行いたい場合です。Vuexは、Vueの公式状態管理ライブラリであり、Vueのコンポーネントと密接に連携しています。

redux のREADME

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