Socket
Socket
Sign inDemoInstall

redux-saga-promisify-actions

Package Overview
Dependencies
12
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-saga-promisify-actions

Make promise from your action creator for use it in your components


Version published
Maintainers
1
Install size
66.4 kB
Created

Readme

Source

redux-saga-promisify-actions

Motivation

When you make side effects in the saga, your component doesn't know about data flow, for this reason, this package makes a promise from your action creator like redux-thunk. It's makes possible use any form state management package without tears.

Installation

 yarn add redux-saga-promisify-actions

Usage

Functions syncAction and asyncAction make one action creator and encapsulated types.

syncAction
 import { syncAction } from 'redux-saga-promisify-actions'
 
 // make action creator
 const getItem = syncAction('ITEM/GET_ITEM')
 
 getItem.type === 'ITEM/GET_ITEM'
 
 getItem({ data: 'data' }) === { type: 'ITEM/GET_ITEM', payload: { data: 'data' } }

For making custom action structure use second param

const getItem = syncAction('ITEM/GET_ITEM', (data) => ({ data, params: data.params }))

getItem({ data: { params: [1, 2] } }) === { type: 'ITEM/GET_ITEM', data: { params: [1, 2] }, params: [1, 2] }
asyncAction

It's similar to syncAction but with types: request, success, failure

import { asyncAction } from 'redux-saga-promisify-actions'

const getItem = asyncAction('ITEM/GET_ITEM')

getItem.request === 'ITEM/GET_ITEM/REQUEST'

getItem.success === 'ITEM/GET_ITEM/SUCCESS'

getItem.failure === 'ITEM/GET_ITEM/FAILURE'

Wrap action into promise

Installation

In your create store function

import { actionsPromiseWatcherSaga } from "redux-saga-promisify-actions";

sagaMiddleware.run(actionsPromiseWatcherSaga);

In your components:

import { bindPromiseToAction } from "redux-saga-promisify-actions";

class MyComponent extends Component {
  componentDidMount() {
    this.props
      .getItems({ data: "data" })
      .then((payload) => console.log("success", payload)) // { data: "data" }
      .catch((payload) => console.log("reject", payload));
  }
  ...
  render() {
    ...
  }
  }
  
  export default connect(
    null,
    (dispatch) => ({ getItems: bindPromiseToAction(getItems, dispatch) })
  )(MyComponent);

Keywords

FAQs

Last updated on 30 Jan 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc