GithubHelp home page GithubHelp logo

darmikon / ducks-helpers Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 11 KB

Some functions which helps to reduce boilerplate in redux ducks files

JavaScript 100.00%
redux redux-actions ducks-helpers ducks react reactjs react-redux

ducks-helpers's Introduction

ducks-helpers

Utils for ducks in redux to create action types and actions creators

###Installation

npm i --save ducks-helpers

###constants(namespace, actions) constants() - generates action types names If ~ sign presents at the beginning of the string then extra sufixes will be generated:

// available SUFFIXES

// 'LOADING'
// 'PENDING'
// 'SUCCESS'
// 'ERROR'
// 'FAILED'
// 'CANCELED'

How to use:

import {constants} from 'ducks-helpers'
export const TYPE = constants('module-name/namespace', [
    '~ASYNC_ACTION',
    'SYNC_ACTION'
])

Result:

// with ~
// TYPE.ASYNC_ACTION === 'module-name/namespace/ASYNC_ACTION'
// TYPE.ASYNC_ACTION_SUCCESS === 'module-name/namespace/ASYNC_ACTION_SUCCESS'
//...
// TYPE.ASYNC_ACTION_CANCELED === 'module-name/namespace/ASYNC_ACTION_CANCELED'

// without ~
// TYPE.SYNC_ACTION === 'module-name/namespace/SYNC_ACTION'

Using handleActions() from redux-actions

// reducer
export default handleActions({
    [TYPE.SYNC_ACTION]: (state, action) => state,
    [TYPE.ASYNC_ACTION]: (state, action) => state,
    [TYPE.ASYNC_ACTION_LOADING]: (state, action) => state,
    //... other suffixes also are available and can be used
    [TYPE.ASYNC_ACTION_SUCCESS]: (state, action) => state,
    [TYPE.ASYNC_ACTION_ERROR]: (state, action) => state,
}, {})

###actions(types)

types is an array of constants. In case of using ~ sign at the beginning of action name it will also create action creators with suffixes syncAction, asyncAction, asyncActionLoading, asyncActionPending, asyncActionCanceled, asyncActionError, asyncActionFailed using redux-actions:

import {actions} from 'ducks-helpers'
export const ACTION = actions(TYPE)

All action creators have been built now. You can use any action creators in your container.

// container.js
import { ACTION } from '../duck'
...
@connect(
    ...,
    {
        asyncAction: ACTION.asyncAction
    }

)
...

componentWillMount(){
    this.props.asyncAction()
}

...

ducks-helpers's People

Stargazers

 avatar  avatar

Watchers

 avatar  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.