Code Monkey home page Code Monkey logo

redux-functions's Introduction

Redux Functions

A set of functions allow you to create action-creators, reducers, action-type faster and type-safer.

npm

Installation

NPM

npm i redux-functions

Yarn

yarn add redux-functions

CDN

<!-- format https://cdn.jsdelivr.net/npm/redux-functions@{{VERSION}}/umd/main.js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/main.js"></script>

How to Use

toType

In a large scale of redux application, there are tons of action types. It is always a nightmare for developers to think of an unique action name.

toType allows you to add a prefix for the action name, so it is easier to debug and manage.

For example

import { toType } from "redux-functions";

// create an action type generator
const type = toType("TABLE_TENNIS");

// export the actions type for later use
export const PING = type("PING"); // "TABLE_TENNIS::PING"
export const PONG = type("PONG"); // "TABLE_TENNIS::PONG"

toActionCreator

To create an action creator is very simple,

import { toActionCreator } from "redux-functions";

export const ping = toActionCreator("PING");
// In Typescript, you can also create a type-safe action creator
export const pong = toActionCreator<boolean>("PONG");

// to use it
ping() // { type: "PING" }
pong(true) // { type: "DELAY_PONG", payload: true };

Inspired by redux-toolkit, You can also use the action creator to verify an unknown action.

// In Javascript, using "=="
ping == { type: "PING" } // true
ping == { type: "PONG" } // false

// In Typescript
ping.test({ type: "PING" }) // true
ping.test({ type: "PONG" }) // false

Caveat

There is a caveat about the redux actions generated by redux-functions.

All the actions generated by toActionCreator will be in the format of

import type { Action } from "redux";
interface AppAction<T> extends Action<string>{
    payload: T;
}

toReducer

To create a reducer without boilerplate. toReducer takes two parameters, action type and default value, i.e. toReducer(action, defaultValue).

For example

import { toReducer } from "redux-functions";
import { combineReducers } from "redux";

const ping = toReducer("PING", false);
const pong = toReducer("PONG", false);

const reducers = combineReducers({ ping, pong });

Caveat

Unlike redux-toolkit's createReducer, toReducer only handle one single action for each generated reducer.

If you would like to have more customisation on the reducer handling, please check redux-toolkit.

redux-functions's People

Contributors

samsan1212 avatar

Stargazers

 avatar  avatar

Watchers

 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.