Coder Social home page Coder Social logo

vue-next-webpack-preview's Introduction

Nuex

This state management library is fully type complete and managed within the ecosystem of TypeScript

Creating a store

import createStore, { mutation, action } from 'nuex';

const store = createStore({
  state: {
    counter: 0,
  },
  init(state) {
    // state is reactive
    const increment = mutation('increment', () => state.count++);

    return {
      state,
      increment,
    };
  },
});

store.increment(); // Commits the mutation "increment"

Component usage

You'll want to use your state, right? So you need a way to get it into your app. Use the included storeProvider() function to create helpers for injecting the store into your components.

store.js

import { createStore, storeProvider, ... } from 'nuex'

const store = createStore({
  state: {
    counter: 0,
  },
  init(state) {
    const increment = mutation(() => state.count++);

    return {
      state,
      increment,
    };
  },
});

export const { provideStore, useStore } = storeProvider(store)

App.vue

<template>
  ...
</template>

<script>
  import { provideStore } from '@/store';

  export default {
    setup() {
      // Provide the store from your root component to all child components
      provideStore();
    },
  };
</script>

Component.vue

<template>
  <span>{{ counter }}</span>
  <button @click="">Increment</button>
</template>

<script>
  import { useStore } from '@/store';

  export default {
    setup() {
      const { state, increment } = useStore();

      return {
        increment,
      };
    },
  };
</script>

Mutations

Mutations are synchronous operations that mutate the state of a store. They are used to label important operations on your data. Mutations can help debug problems and trace down when and why state is changing. Mutations are optional in Nuex, since all cahnges are automatically tracked. but they can be helpful in the debugger.

In Nuex, mutations can be given a name as the first argument or they inherit their name when they are returned from init().

import { createStore, mutation } from 'nuex';

const store = createStore({
  state: {
    counter: 0,
  },
  init(state) {
    const increment = mutation('INCREMENT', () => state.counter++);
    const decrement = mutation(() => state.counter--);
    const clear = mutation(() => {
      state.counter = 0;
    });

    return {
      state,
      increment,
      decrement,
      reset: clear,
    };
  },
});

store.increment(); // commits the "INCREMENT" mutation
store.decrement(); // commits the "decrement" mutation
store.reset(); // commits the "reset" mutation

Actions

Actions are just functions that call mutations, they can be asynchronous too!

You can either make actions with regular functions or import the action function from Nuex.

import { createStore, mutation, action } from 'nuex'
import getUser from './dataSource'

const store = createStore({
  state: {
    loading: false
    user: null,
  },
  init(state) {

    const fetchUser = action(async () => {
      state.loading = true
      try {
        state.user = await getUser()
      } finally {
        state.loading = false
      }
    })

    return {
      fetchUser,
    };
  },
});

store.fetchUser(); // calls the "fetchUser" action which changes the loading and user state.

vue-next-webpack-preview's People

Contributors

jexordexan avatar nek4life avatar yyx990803 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.