Convert Figma logo to code with AI

vuejs logovuex

🗃️ Centralized State Management for Vue.js.

28,416
9,567
28,416
143

Top Related Projects

27,458

Simple, scalable state management.

Reactive State for Angular

The official, opinionated, batteries-included toolset for efficient Redux development

8,475

The Redux Framework

30,605

A reactive programming library for JavaScript

Quick Overview

Vuex is the official state management library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

Pros

  • Centralized state management for Vue applications
  • Provides a clear and organized structure for managing complex application states
  • Integrates seamlessly with Vue's reactivity system
  • Includes devtools support for easy debugging and time-travel

Cons

  • Adds complexity to smaller applications where simple state management would suffice
  • Requires additional boilerplate code compared to using Vue's built-in reactivity system
  • Learning curve for developers new to state management patterns
  • Can lead to over-engineering if not used judiciously

Code Examples

  1. Creating a Vuex store:
import { createStore } from 'vuex'

const store = createStore({
  state() {
    return {
      count: 0
    }
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})
  1. Using Vuex in a Vue component:
import { useStore } from 'vuex'

export default {
  setup() {
    const store = useStore()
    
    return {
      count: computed(() => store.state.count),
      increment: () => store.commit('increment')
    }
  }
}
  1. Defining and using actions:
const store = createStore({
  state: { /* ... */ },
  mutations: { /* ... */ },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  }
})

// In a component
store.dispatch('incrementAsync')

Getting Started

To use Vuex in your Vue.js project:

  1. Install Vuex:

    npm install vuex@next --save
    
  2. Create a store:

    import { createStore } from 'vuex'
    
    const store = createStore({
      state: {
        // your state
      },
      mutations: {
        // your mutations
      },
      actions: {
        // your actions
      },
      getters: {
        // your getters
      }
    })
    
  3. Use the store in your Vue app:

    import { createApp } from 'vue'
    import App from './App.vue'
    import store from './store'
    
    const app = createApp(App)
    app.use(store)
    app.mount('#app')
    

Competitor Comparisons

27,458

Simple, scalable state management.

Pros of MobX

  • More flexible and less opinionated, allowing for easier integration with existing projects
  • Simpler API with less boilerplate code, leading to faster development
  • Automatic tracking of observables, reducing the need for manual updates

Cons of MobX

  • Less structured approach may lead to inconsistent code patterns across larger projects
  • Steeper learning curve for developers new to reactive programming concepts
  • Potential for performance issues if not used carefully with large datasets

Code Comparison

Vuex:

const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  }
})

MobX:

const store = observable({
  count: 0,
  increment() { this.count++ }
})

In this comparison, MobX demonstrates a more concise syntax for creating and updating observable state. Vuex requires a more structured approach with separate state and mutations objects, while MobX allows for a more direct definition of observable properties and actions.

Both libraries offer powerful state management solutions, but MobX provides more flexibility at the cost of potential complexity in larger applications. Vuex, on the other hand, enforces a stricter structure that can be beneficial for maintaining consistency in Vue.js projects.

Reactive State for Angular

Pros of platform

  • More robust and feature-rich, offering a complete state management solution
  • Better suited for large-scale applications with complex state management needs
  • Provides powerful dev tools for debugging and time-travel debugging

Cons of platform

  • Steeper learning curve due to its complexity and RxJS integration
  • More boilerplate code required compared to Vuex's simpler approach
  • Can be overkill for smaller applications or simpler state management needs

Code Comparison

platform (NgRx):

@Component({...})
export class MyComponent {
  todos$ = this.store.select(selectAllTodos);
  constructor(private store: Store) {}
  addTodo(title: string) {
    this.store.dispatch(addTodo({ title }));
  }
}

Vuex:

export default {
  computed: {
    ...mapState(['todos'])
  },
  methods: {
    addTodo(title) {
      this.$store.commit('ADD_TODO', { title })
    }
  }
}

The platform code demonstrates the use of observables and the dispatch of actions, while Vuex uses a more straightforward approach with state mapping and mutations. platform's approach is more verbose but offers better type safety and scalability for complex applications.

The official, opinionated, batteries-included toolset for efficient Redux development

Pros of Redux Toolkit

  • Includes utilities for common Redux use cases, reducing boilerplate code
  • Provides a more opinionated structure, making it easier for beginners
  • Integrates seamlessly with React applications

Cons of Redux Toolkit

  • Steeper learning curve for developers new to Redux concepts
  • More verbose setup compared to Vuex's simplicity
  • Requires additional dependencies, increasing bundle size

Code Comparison

Redux Toolkit:

import { createSlice } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: state => state + 1,
  },
})

Vuex:

export default {
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
}

Key Differences

  • Redux Toolkit uses a more functional approach with immutable updates
  • Vuex relies on mutations for state changes, which are more intuitive for Vue developers
  • Redux Toolkit's createSlice combines actions and reducers, while Vuex separates state, mutations, and actions

Conclusion

Both Redux Toolkit and Vuex are powerful state management solutions. Redux Toolkit offers more features and is well-suited for complex React applications, while Vuex provides a simpler, more Vue-centric approach. The choice between them often depends on the specific project requirements and the development team's familiarity with each ecosystem.

8,475

The Redux Framework

Pros of Rematch

  • Simpler API with less boilerplate code
  • Built-in support for async actions without middleware
  • TypeScript-friendly with better type inference

Cons of Rematch

  • Smaller community and ecosystem compared to Vuex
  • Less Vue-specific optimizations and integrations
  • Steeper learning curve for developers already familiar with Vuex

Code Comparison

Vuex:

const store = new Vuex.Store({
  state: { count: 0 },
  mutations: {
    increment(state) { state.count++ }
  },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => { commit('increment') }, 1000)
    }
  }
})

Rematch:

const store = init({
  models: {
    count: {
      state: 0,
      reducers: {
        increment(state) { return state + 1 }
      },
      effects: (dispatch) => ({
        async incrementAsync() {
          await new Promise(resolve => setTimeout(resolve, 1000))
          dispatch.count.increment()
        }
      })
    }
  }
})

Both Vuex and Rematch are state management libraries, but Rematch offers a more streamlined API with built-in async support. Vuex, being Vue-specific, has better integration with Vue ecosystems. Rematch's approach may be more appealing to developers coming from other frameworks, while Vuex remains the go-to choice for many Vue developers due to its mature ecosystem and Vue-optimized design.

30,605

A reactive programming library for JavaScript

Pros of RxJS

  • More powerful and flexible for complex asynchronous operations
  • Language-agnostic, can be used with various frameworks and libraries
  • Extensive set of operators for data transformation and manipulation

Cons of RxJS

  • Steeper learning curve due to its complexity
  • Can be overkill for simpler state management needs
  • Requires more boilerplate code for basic operations

Code Comparison

RxJS:

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

const source$ = new Observable(observer => {
  observer.next(1);
}).pipe(map(x => x * 2));

Vuex:

import { createStore } from 'vuex';

const store = createStore({
  state: { count: 1 },
  mutations: { increment(state) { state.count++ } }
});

Summary

RxJS is a powerful reactive programming library that excels in handling complex asynchronous operations and data streams. It offers a wide range of operators and is framework-agnostic. However, it has a steeper learning curve and can be excessive for simple state management.

Vuex, on the other hand, is specifically designed for Vue.js applications and provides a simpler, more straightforward approach to state management. It's easier to learn and implement for basic to moderate complexity applications but may lack the advanced features and flexibility of RxJS for more complex scenarios.

The choice between the two depends on the specific needs of your project, the complexity of your state management requirements, and your familiarity with reactive programming concepts.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Vuex

npm ci status


Pinia is now the new default

The official state management library for Vue has changed to Pinia. Pinia has almost the exact same or enhanced API as Vuex 5, described in Vuex 5 RFC. You could simply consider Pinia as Vuex 5 with a different name. Pinia also works with Vue 2.x as well.

Vuex 3 and 4 will still be maintained. However, it's unlikely to add new functionalities to it. Vuex and Pinia can be installed in the same project. If you're migrating existing Vuex app to Pinia, it might be a suitable option. However, if you're planning to start a new project, we highly recommend using Pinia instead.


Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion. It also integrates with Vue's official devtools extension to provide advanced features such as zero-config time-travel debugging and state snapshot export / import.

Learn more about Vuex at "What is Vuex?", or get started by looking into full documentation.

Documentation

To check out docs, visit vuex.vuejs.org.

Examples

You may find example applications built with Vuex under the examples directory.

Running the examples:

$ npm install
$ npm run dev # serve examples at localhost:8080

Questions

For questions and support please use the Discord chat server or the official forum. The issue list of this repo is exclusively for bug reports and feature requests.

Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

Changelog

Detailed changes for each release are documented in the release notes.

Stay In Touch

For latest releases and announcements, follow on Twitter: @vuejs.

Contribution

Please make sure to read the Contributing Guide before making a pull request.

License

MIT

Copyright (c) 2015-present Evan You

NPM DownloadsLast 30 Days