DEV Community

Cover image for Redux Actions - Without declaring CONSTANTS and action-creator functions for each action
Ruben Arushanyan
Ruben Arushanyan

Posted on

5

Redux Actions - Without declaring CONSTANTS and action-creator functions for each action

Actions Creator

You can find the full documentation on the https://actions-creator.js.org

Description

actions-creator is an awesome javascript package that allows you to dynamically create action objects in Redux without having to declare CONSTANTS and separate action-creator functions for each action.

Installation

npm install actions-creator
Enter fullscreen mode Exit fullscreen mode

Usage Example

import {actionsCreator} from 'actions-creator'

const action_1 = actionsCreator.MY.FIRST.ACTION('arg1')
//      {
//          type: 'MY/FIRST/ACTION',
//          payload: 'arg1',
//      } 

const action_2 = actionsCreator.This.is.my.second.action(2021)
//      {
//          type: 'This/is/my/second/action',
//          payload: 2021,
//      } 


// To get the type of action
String( actionsCreator.MY.FIRST.ACTION ) // 'MY/FIRST/ACTION'
// or
actionsCreator.MY.FIRST.ACTION().type // 'MY/FIRST/ACTION'

Enter fullscreen mode Exit fullscreen mode

Actions With Callback

Sometimes we need the action to have callback capability. It might be necessary in many cases.

Actions Creator allows us to do this in a beautiful way:
When we try to generate an action object, we can pass the callback function as the last argument. Actions Creator will check and if the last argument is a function, it will be considered as a callback function.

import {actionsCreator} from 'actions-creator'

const callback = () => {
    console.log('Hello, I am callback!!!')
}

const action = actionsCreator.MY.CALLBACKABLE.ACTION(123, callback)
//      {
//          type: 'MY/CALLBACKABLE/ACTION',
//          payload: 123,
//          cb: [Function callback],
//      }

action.cb() // 'Hello, I am callback!!!'
Enter fullscreen mode Exit fullscreen mode

Syntax

actionsCreator.ANY.ACTION(payload, callback?)

  • payload <Any> Any value as a payload.
  • callback <Function> Any function as a callback.

Returns <Object>

  • type <String> Action type as a string.

    actionsCreator.ANY.ACTION().type === 'ANY/ACTION' // true
    
  • payload <Any>

    The value of the payload - given in the first argument.

    actionsCreator.ANY.ACTION(123).payload === 123 // true
    
  • cb <Function>

    The function of the callback - given in the last argument.

    const callback = () => {};
    
    actionsCreator.ANY.ACTION(123, callback).cb === callback // true
    

Documentation

Links

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (3)

Collapse
 
guscarpim profile image
Gustavo Scarpim

Nice!

Collapse
 
markerikson profile image
Mark Erikson

Pretty sure this has come up in prior threads, but:

the right answer is to use our official Redux Toolkit package and its createSlice API, which automatically generates action creators for each case reducer function that you write:

import { createSlice } from '@reduxjs/toolkit'

const todosSlice = createSlice({
  name: 'todos',
  initialState: [],
  reducers: {
    todoAdded(state, action) {
      state.push({
        id: action.payload.id,
        text: action.payload.text,
        completed: false
      })
    },
    todoToggled(state, action) {
      const todo = state.find(todo => todo.id === action.payload)
      todo.completed = !todo.completed
    }
  }
})

export const { todoAdded, todoToggled } = todosSlice.actions
export default todosSlice.reducer
Enter fullscreen mode Exit fullscreen mode

More details on how and why to use Redux Toolkit:

Collapse
 
xr0master profile image
Sergey Khomushin

Or you can just use TypeScript :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay