1.0.3 • Published 4 years ago

vue-use-reducer v1.0.3

Weekly downloads
13
License
MIT
Repository
github
Last release
4 years ago

Vue useReducer

npm version License: MIT CI Status

Inspired by useReducer of React Hooks.

Install

$ yarn add vue-use-reducer

Usage

Usage is the same as useReducer of React Hooks.

store

import { useReducer } from 'vue-use-reducer';

const initialState = {
  count: 0,
};

const reducer = (state, action) => {
  switch (action.type) {
    case 'increment':
      return {
        ...state,
        count: state.count + 1,
      };
    case 'decrement':
      return {
        ...state,
        count: state.count - 1,
      };
  }
};

const [state, dispatch] = useReducer(reducer, initialState);

export const counterState = state;
export const counterDispatch = dispatch;

component

<template>
  <div>
    <h1>Use Reducer Sample Counter</h1>

    <div>
      <span>{{ count }}</span>
    </div>
    <div>
      <button type="button" @click="increment">+1</button>
      <button type="button" @click="decrement">-1</button>
    </div>
  </div>
</template>

<script>
  import { counterState, counterDispatch } from '@/store/counter';

  export default {
    computed: {
      count() {
        return counterState.count;
      },
    },

    methods: {
      increment() {
        counterDispatch({ type: 'increment' });
      },

      decrement() {
        counterDispatch({ type: 'decrement' });
      },
    },
  };
</script>

Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.

License

MIT

2.0.1-0

4 years ago

2.0.0-3

4 years ago

2.0.0-2

4 years ago

2.0.0-1

4 years ago

2.0.0-0

4 years ago

1.0.3

4 years ago

1.0.3-0

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago