All Projects → olegman → redux-actions-helpers

olegman / redux-actions-helpers

Licence: MIT license
Small helpers functions for creating and handle redux actions

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to redux-actions-helpers

flux-action-class
Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works with redux and ngrx.
Stars: ✭ 22 (-37.14%)
Mutual labels:  redux-actions
typed-actions
Type-safe redux-actions
Stars: ✭ 43 (+22.86%)
Mutual labels:  redux-actions
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-8.57%)
Mutual labels:  redux-actions
fetch-action-creator
Fetches using standardized, four-part asynchronous actions for redux-thunk.
Stars: ✭ 28 (-20%)
Mutual labels:  redux-actions
redux-tools
💪 Maintaining large Redux applications with ease.
Stars: ✭ 34 (-2.86%)
Mutual labels:  redux-actions
redux-thunk-actions
Action creator for redux-thunk that handles sync and async functions.
Stars: ✭ 63 (+80%)
Mutual labels:  redux-actions
Typesafe Actions
Typesafe utilities for "action-creators" in Redux / Flux Architecture
Stars: ✭ 2,343 (+6594.29%)
Mutual labels:  redux-actions
redux-action
🍻 Flux action utilities for Redux
Stars: ✭ 25 (-28.57%)
Mutual labels:  redux-actions
redux-routines
👷 Predefined actions creators and action types for common async tasks in a single object called "routine"
Stars: ✭ 12 (-65.71%)
Mutual labels:  redux-actions

Redux Actions Helpers

Build Status Latest Stable Version Dependency Status License NPM Downloads NPM Downloads

Redux actions helpers helps you create redux actions with less code and avoid common mistakes. Jump to QuickStart Guide to see it in action.

  • Simple: it's only helpers for creating and handle actions from official redux documentation nothing more, I don't try to invent some new way of creating and handle actions and that's why it easy to integrate in existing project and it can work nice with other tools and libs.
  • Small: it's really very small, without any extra dependencies and won't bloat your bundle size.
  • Productive: the main goal of this lib to keep you more productive, you will write much less code and do more, also you have to do much less job when you do some refactorings and it provide some very helpful errors and warnings to avoid common mistakes and save your time on debugging.

Installation

npm install --save redux-actions-helpers

QuickStart Guide

Let's create an action first, in good old days it can look like this:

export const ACTION_WITHOUT_PARAMS = 'ACTION_WITHOUT_PARAMS';
export function actionWithoutParams() {
    return {
        type: ACTION_WITHOUT_PARAMS
    }
}

But now we have createAction helper that could do it much better:

import { createAction } from 'redux-actions-helpers';
export const actionWithoutParams = createAction('ACTION_WITHOUT_PARAMS');

Feel the difference only one single line to create action(it's 83,4% less lines of code), and as you can see we moved away extra export of constant, because for now our action can act like constant too.

And what about actions with params, previously we can do like this

export const ACTION_WITH_PARAMS = 'ACTION_WITH_PARAMS';
export function actionWithParams(param1, param2) {
    return {
        type: ACTION_WITH_PARAMS,
        param1,
        param2
    }
}

For now it can be easy as:

import { createAction } from 'redux-actions-helpers';
export const actionWithParams = createAction('ACTION_WITH_PARAMS', (param1, param2) => ({ param1, param2 }));

Ok, looks good now let's move on in our reducer and look how can we get benefit from our new and shiny actions. And here we have another helper method handleActions:

import { handleActions } from 'redux-actions-helpers';
import { actionWithoutParams, actionWithParams, anotherAction } from 'actions.js';

const initialState = {
    param1: 'someValue',
    param2: 'someValue'
};

export default handleActions({
    /* yay we use one function to dispatch actions and like constant, looks nice.
    Under the hood it's just a constant so if we write 'ACTION_WITHOUT_PARAMS' this will work too */
    [actionWithoutParams]: (state, action) => {
        // some cool stuff
        return {
            ...state,
            withCoolStuff
        };
    },
    /* 💥 killer feature: multiple actions on one handler with very nice and short syntax.
    But nothing magical under the hood it's just constants with ',' delimeter */
    [actionWithParams + anotherAction]: (state, action) => {
        let { param1, param2 } = action;
        return {
            ...state,
            param1,
            param2
        };
    }
}, { initialState });

That's all, that simple 😎

What about async actions?

As I mentioned earlier this lib works well with existing ecosystem and the best way to work with async actions is redux-saga you must check it out if you haven't already 😉

Example with redux saga

// saga.js
import { call, put } from 'redux-saga/effects';
import { takeEvery } from 'redux-saga';
import { fetch, fetchSuccess, fetchFail } from 'actions.js';

export function* fetchData(action) {
   try {
      const data = yield call(Api.fetchData, action.url);
      yield put(fetchSuccess(data));
   } catch (error) {
      yield put(fetchFail(error));
   }
}

export default function* rootSaga() {
  yield fork(takeEvery, fetch.toString(), fetchData)
}

Everything looks nice instead of this string: fetch.toString(). The problem is that action can act like constant in some situation with no effort but it still a function and in some cases you need to force action to be a constant with type string. But I'm very kind and have one more thing for you, this is redux-actions-helpers-saga. For now this is only one small function that just an alias for redux-saga fork helper function, that do .toString transform under the hood, to keep code looks pretty and nice:

import { forkActionWatcher } from 'redux-actions-helpers-saga';
export default function* rootSaga() {
  yield forkActionWatcher(takeEvery, fetch, fetchData)
}

Yep, much better 👏

API Reference

###createAction(type, paramsCreator)

  • type: String: action id, must be unique.
  • paramsCreator: Function: returns object of params that will be merged to action itself.

###handleActions(handlers, options)

  • handlers: Object: just plain object where key must be a string and value function handler.
  • options: Object: object with some options.

Available options:

  • initialState: required option that must provide your initialState (defaultState) state of reducer.
  • strict: default value is true, enables strict mode with all available warnings.

###clearRegistry()

Errors and warnings Reference

  • Error: Duplicate Action: your type property in action must be unique to avoid some unexpected behaviour, and if you have such mistake you will get an error to fix this. If you have many actions in many places(components) the best practice to avoid this error is naming action with namespace like so: @@namespace/OUR_CONSTANT
  • Warning: Property type is reserved by action: means that you create params with type property, but action already resrved this property to itself and won't be overriden. So you need to rename this property to different name.
  • Warning: Unknown action: this warning can produce handleActions function when you pass in some unknown action type.

Conclusion

I know there are many similar libs that try to solve same problem, for example: redux-actions, redux-act and many more. But in fact it's not true, because they try to solve many problems at once and looks too complicated with many unnecessary things for me (that bloat my final bundle size 😱), moreover they don't have some useful things that my library can do. My library do small things but do it very good, it's really save your time, help with useful errors and warnings, and have low cost to integrate in existing solution and even can reduce your final bundle size (if you have many actions in project). Hope you enjoy it, as I do ✌️

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].