Socket
Book a DemoInstallSign in
Socket

reddeck

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reddeck

A couple of Redux utils that make life easier and less boilerplaty

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

reddeck

npm version npm downloads Build Coverage Status code style: prettier

A couple of redux utils I wrote to make my life easier and reduce boilerplate.

Install

yarn add reddeck

API

actionCreator(type)

Takes a string as parameter, returns an "action creator". An action creator is a function that returns a { type, payload } object when called.

import { actionCreator } from 'reddeck';

const setName = actionCreator('SET_NAME');
console.log(setName('Foo')); // { type: 'SET_NAME', payload: 'Foo' }

asyncActionCreator(pendingType, successType, errorType)

Similar to the actionCreator() function, but instead of returning an action creator, it returns an object with 3 action creators: pending(), success() and error().

import { asyncActionCreator } from 'reddeck';

const getData = asyncActionCreator(
  'GET_DATA_PENDING',
  'GET_DATA_SUCCESS',
  'GET_DATA_ERROR'
);

It's useful when you want to handle an asynchronous control flow

const get = async () => {
  try {
    dispatch(getData.pending()); // { type: 'GET_DATA_PENDING', payload: {} }
    const data = await getDataFromAPI();
    dispatch(getData.success(data)); // { type: 'GET_DATA_SUCCESS', payload: data }
  } catch (err) {
    dispatch(getData.error(err)); // { type: 'GET_DATA_ERROR', payload: err }
  }
};

typeCreator(types, options)

First param is a string containing all the types. Spaces are ignored. The second parameter specifies the options and it's optional.

import { typeCreator } from 'reddeck';

const options = {
  prefix: 'app/'
};

const types = typeCreator(
  `
  SET_NAME

  GET_DATA_PENDING
  GET_DATA_SUCCESS
  GET_DATA_ERROR
`,
  options
);

console.log(types);
/*
  {
    SET_NAME: 'app/SET_NAME',
    GET_DATA_PENDING: 'app/GET_DATA_PENDING',
    GET_DATA_SUCCESS: 'app/GET_DATA_SUCCESS',
    GET_DATA_ERROR: 'app/GET_DATA_ERROR'
  }
*/

apiStateCreator(newState)

import { apiStateCreator } from 'reddeck';

const api = apiStateCreator();
console.log(api);
/*
  {
    pending: false,
    success: false,
    error: false
  }
*/

const apiPending = apiStateCreator({ pending: true });
console.log(apiPending);
/*
  {
    pending: true,
    success: false,
    error: false
  }
*/

Keywords

redux

FAQs

Package last updated on 08 Feb 2021

Did you know?

Socket

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