1.0.3 • Published 3 years ago

storken-actions v1.0.3

Weekly downloads
48
License
MIT
Repository
github
Last release
3 years ago

storken-actions

Add state-specific functions to a state which created by Storken.

Installation

# yarn
yarn add storken-actions

# npm
npm install storken-actions

Example

import { create as createStore } from 'storken'
import Actions from 'storken-actions'

const [useStorken] = createStore({
  plugins: {
    actions: Actions
  },
  storkenOptions: {
    tableData: {
      actions: {
        getUsers: (stork, userId) => {
          return fetch('https://myservice.com/user/' + userId)       
            .then(res => res.json())
        },

        getTransactions: (stork, userId) => {
          return fetch('https://myservice.com/transaction/' + userId)       
            .then(res => res.json())
        }
      }
    }
  }
})

Action functions behave just like getter functions. Its first parameter gives Storken object and the rest parameters are may be action-specific optional parameters.

Actions can also specify like this:

import { create as createStore } from 'storken'
import Actions from 'storken-actions'

const [useStorken] = createStore({
  plugins: {
    actions: Actions
  },
  storkenOptions: {
    tableData: {
      actions: (storken) => ({
        getUser: (userId) => {
          return fetch('https://myservice.com/user/' + userId)       
            .then(res => res.json())
        },

        getTransactions: (userId) => {
          return fetch('https://myservice.com/transactions/' + userId)       
            .then(res => res.json())
        }
      })
    }
  }
})

In this way, actions configuration of the state took a function that returns the actions. When the actions configuration is function (like this), the function's first parameter is storken object and the action functions get their own (action-specific) parameters directly.

License

Distributed under the MIT License.

Contribution

You can contribute by fork this repository and make pull request.

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago