state-types

Tiny and easy creating unique types in popular state-management solutions

Usage no npm install needed!

<script type="module">
  import stateTypes from 'https://cdn.skypack.dev/state-types';
</script>

README

State types

styled with prettier Greenkeeper badge Build Status npm bundle size (minified) npm

Tiny and easy creating unique types in popular state-management solutions

Usage

You can simple generate types object in one function:

import types from 'state-types'

const testTypes = types('@@test', ['foo', 'bar'])

// Will return

{
  'foo': '@@test/foo',
  'bar': '@@test/bar'
}

Vuex mutations, actions, etc.

You can use it in vuex:

import types from 'state-types'

const testMutations = types('@@types', ['foo', 'bar'])

export default {
  state: false,

  mutations: {
    [testMutations.foo]: (state) => state = true,
    [testMutations.bar]: (state) => state = false
  }
}

...

// Import it where you want and use

this.$store.commit(testMutations.foo)

Redux actions types

You can use it in redux:

import types from 'state-types'

export const actionsTypes = types('@@types', ['foo', 'bar'])
export const actionsCreators = {
  [actionsTypes.foo]: () => ({
    type: actionsTypes.foo
  }),
  [actionsTypes.bar]: () => ({
    type: actionsTypes.bar
  })
}

export (state = false, action) => {
  switch(action.type) {
    case actionsTypes.foo:
      return true
    case actionsTypes.bar:
      return false
    default:
      return state
  }
}

...

// Import it where you want and use

dispatch(actionsCreators.foo())

Other cases

You can use it everywhere because it solution hasn't any semantic and code bindings to libraries and frameworks.