All Projects → doniyor2109 → redux-lightweight

doniyor2109 / redux-lightweight

Licence: MIT license
Write one business logic instead of writing actions, action types and reducers

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to redux-lightweight

hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-63.41%)
Mutual labels:  reducer, action
Redux Ecosystem Links
A categorized list of Redux-related addons, libraries, and utilities
Stars: ✭ 5,076 (+12280.49%)
Mutual labels:  reducer, action
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+58.54%)
Mutual labels:  reducer, action
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-65.85%)
Mutual labels:  reducer, action
k-redux-factory
Factory of Redux reducers and their associated actions and selectors.
Stars: ✭ 18 (-56.1%)
Mutual labels:  reducer, action
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+387.8%)
Mutual labels:  reducer, action
Redux
No description or website provided.
Stars: ✭ 40 (-2.44%)
Mutual labels:  reducer
chrome-extension-upload
upload & publish extensions to the Chrome Web Store.
Stars: ✭ 35 (-14.63%)
Mutual labels:  action
tectonic-action
Compile Tex files easily
Stars: ✭ 77 (+87.8%)
Mutual labels:  action
redux-entities
Higher-order reducer for store entities received from normalizr and makes it easy to handle them.
Stars: ✭ 34 (-17.07%)
Mutual labels:  reducer
redux-interactions
Composing UI as a set of interactions
Stars: ✭ 22 (-46.34%)
Mutual labels:  reducer
IndRNN pytorch
Independently Recurrent Neural Networks (IndRNN) implemented in pytorch.
Stars: ✭ 112 (+173.17%)
Mutual labels:  action
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-21.95%)
Mutual labels:  reducer
GitHub-Pages-deploy
A GitHub Action to deploy a static site on GitHub Pages.
Stars: ✭ 42 (+2.44%)
Mutual labels:  action
Vapecord-ACNL-Plugin
Animal Crossing NL Vapecord Public Plugin WIP
Stars: ✭ 72 (+75.61%)
Mutual labels:  action
action-homebrew-bump-formula
⚙️ A GitHub Action to easily bump Homebrew formula on new release
Stars: ✭ 68 (+65.85%)
Mutual labels:  action
setup-jdk
(DEPRECATED) Set up your GitHub Actions workflow with a specific version of AdoptOpenJDK
Stars: ✭ 32 (-21.95%)
Mutual labels:  action
action-dynamic-readme
~ Dynamic ReadME Generator ~
Stars: ✭ 29 (-29.27%)
Mutual labels:  action
setup-bats
GitHub Action to setup BATS testing framework
Stars: ✭ 25 (-39.02%)
Mutual labels:  action
dart-package-publisher
Action to Publish Dart / Flutter Package To https://pub.dev When you need to publish a package, just bump the version in pubspec.yaml
Stars: ✭ 45 (+9.76%)
Mutual labels:  action

redux-lightweight

This library generates actions creators, action types and reducers for you. It uses class as a syntactic sugar for generating actions and reducers.

Build Status codecov PRs Welcome GitHub

Table of Contents

Introduction

Motivation

Redux is great library which solves data management. However it introduces some boilerplate. In order to add one business logic, developer must create 3 different things (action type, action creator, reducer) and they do one thing together. That is why I have decide to create utility that allows declare them in one place. One business logic should be declared in one place.

This library is inspired by redux-actions and mobx

Getting Started

Installation

$ npm install --save redux-lightweight

or

$ yarn add redux-lightweight

Usage

Create class that has state property as initial state and methods as actions.

import { createUpdater } from 'redux-lightweight';

export class Counter {
  state = 10;
  
  increment(amount = 1) {
    return this.state + amount;
  }
  
  decrement(amount = 1) {
    return this.state - amount;
  }
}

export const [counterReducer, counterActions] = createUpdater(Counter);

counterReducer; //  reducer for Counter class
counterActions; //  action creator for Counter  class - { increment, decrement }

Usage with React Hooks

redux-lightweight exposes useUpdater custom hook to make it easier working with reducers.

import React from 'react';
import { useUpdater } from 'redux-lightweight';

import { Counter } from './Counter';

function CounterView() {
    const [counter, { increment, decrement }] = useUpdater(Counter);
    return (
        <div>
            <p>{counter}</p>
            <button onClick={() => increment()}>+</button>
            <button onClick={() => decrement()}>-</button>
        </div>
    );
}

Edit 0y50x9040v

Usage with react-redux

redux-lightweight generates simple action creators and reducer. So you can work with them as usual.

import React from 'react';
import { connect } from 'react-redux';

import { counterActions } from './Counter';

function Counter({ counter, increment, decrement }) {
    return (
        <div>
            <p>{counter}</p>
            <button onClick={increment}>+</button>
            <button onClick={decrement}>-</button>
        </div>
    );
}

const mapStateToProps = (state) => ({ counter: state });

const mapDispatchToProps = {
  increment: counterActions.increment,
  decrement: counterActions.decrement
};

export default connect(mapStateToProps, mapDispatchToProps)(Counter);

Edit 0y50x9040v

Usage with redux-saga

In order to handle redux-lightweight generated action creators in saga, you can access action type with action function itself:

import { takeEvery } from 'redux-saga/effects';

import { counterActions } from './Counter';

function* rootSaga() {
  yield takeEvery(counterActions.increment.type, incrementWorkerSaga);
}

Usage only for actions

If you have already big reducers that are difficult to migrate then you can use library as action generator.

Arguments passed to actions will be array in payload

{
  type: actionType,
  payload: args // array of arguments
}
export class Counter {
  increment(amount) {}
  
  decrement(amount) {}
}

const [, counterActions] = createUpdater(Counter);

switch(type, payload) {
   case counterActions.increment.type:
    return state + payload[0];
  case counterActions.decrement.type:
    return state - payload[0];
   default:
    return state;
}

dispatch(counterActions.increment(1));

Advanced Usage

As redux-lightweight works with classes, you can use extended classes as well. That is useful to reuse reducers and actions.

class Calculator extends Counter {
  double() {
    return this.state * 2;
  }
}

export const [calculatorReducer, calculatorActions] = createUpdater(Calculator);

Now it generates 3 action creators:

  • increment
  • decrement
  • double

How it works

Basically redux-lightweight generates action creators, action types and reducers for you.

When you pass your class to redux-lightweight, it generates following things for you:

  • Action creator - Each method of class e.g increment, decrement
  • Action type - Prefixed by class name e.g "Counter/increment"
  • Reducer - Which handles all actions inside class
    • In order to set initial state for reducer, declare state property on class.
class Counter {
  state = 10; // Initial state for reducer = 10
  
  increment(amount = 1) {
    return this.state + amount;
  }
  
  decrement(amount = 1) {
    return this.state - amount;
  }
}

const [counterReducer, counterActions] = createUpdater(Counter)
  • counterActions contains all methods of Counter class as actions. In this case there will be two actions:
counterActions.increment(100) // { type: "Counter/increment", args: [100] }

counterActions.decrement(100) // { type: "Counter/decrement", args: [100] }
  • counterReducer is reducer that handles all actions of class. It is same as with following switch/case statements:
switch(type) {
   case "Counter/increment":
    return state + amount;
  case "Counter/decrement":
    return state - amount;
   default:
    return state;
}

If you want to get action type for action then you can access it with type property of action:

counterActions.increment.type // "Counter/increment"`

API Reference

createUpdater(Updater)

Creates reducer and action creators for given Updater class. Receives class that has state property and methods.

EXAMPLE
const [reducer, actions] = createUpdater(Counter);

useUpdater(Updater)

Custom hook for using Updater. Receives class that has state property and methods.

EXAMPLE
function App() {
  const [state, actions] = useUpdater(Counter);
}

Licence

This project is licensed under the MIT License - see the LICENSE file for details

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