All Projects → anater → useReduction

anater / useReduction

Licence: other
useReducer without boilerplate

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to useReduction

RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+80.56%)
Mutual labels:  flux, reducer
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+455.56%)
Mutual labels:  flux, reducer
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (+183.33%)
Mutual labels:  flux, reducer
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-61.11%)
Mutual labels:  flux, reducer
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (+197.22%)
Mutual labels:  flux, reducer
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-58.33%)
Mutual labels:  flux, reducer
pojo-observer
A minimalist object observer with React hooks support. Allows you to separate concerns between presentation and interaction logic
Stars: ✭ 91 (+152.78%)
Mutual labels:  react-hooks
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (+166.67%)
Mutual labels:  flux
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (+38.89%)
Mutual labels:  react-hooks
aimscroll
🍹 Painless utility libary to handle scroll positions and methods in react
Stars: ✭ 12 (-66.67%)
Mutual labels:  react-hooks
gitops-playground
Reproducible infrastructure to showcase GitOps workflows and evaluate different GitOps Operators on Kubernetes
Stars: ✭ 77 (+113.89%)
Mutual labels:  flux
react-hook-geolocation
A React hook to access data from the Geolocation API
Stars: ✭ 31 (-13.89%)
Mutual labels:  react-hooks
riduce
Get rid of your reducer boilerplate! Zero hassle state management that's typed, flexible and scalable.
Stars: ✭ 14 (-61.11%)
Mutual labels:  reducer
use-fetch-hook
A custom hook to fetch and cache data
Stars: ✭ 95 (+163.89%)
Mutual labels:  react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+177.78%)
Mutual labels:  react-hooks
LatentDiffEq.jl
Latent Differential Equations models in Julia.
Stars: ✭ 34 (-5.56%)
Mutual labels:  flux
Minimal-React
Minimal React + Redux starter kit for experiments.
Stars: ✭ 43 (+19.44%)
Mutual labels:  flux
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-63.89%)
Mutual labels:  react-hooks
acharep
🏠 App to find fraternities near your university
Stars: ✭ 23 (-36.11%)
Mutual labels:  react-hooks
hard-reducer
Type friendly reducer helper
Stars: ✭ 56 (+55.56%)
Mutual labels:  reducer

useReduction

function App() {
  const [count, actions] = useReduction(0, {
    increment: (count, { payload }) => count + payload,
    decrement: (count, { payload }) => count - payload
  });

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => actions.increment(2)}>+2</button>
      <button onClick={() => actions.decrement(2)}>-2</button>
    </div>
  );
}

useReduction minimizes reducer code by automatically creating action creators from your reducer object.

Write less code by using objects instead of switch statements for defining reducers.

Debugging is baked in. In debug mode, all dispatched actions are logged to the console.

Installation

npm install use-reduction

Note: React is a peer dependency. Its assumed you already have this installed for your project.

Usage

import useReduction from "use-reduction";
// Call the hook with an initial state and a reducer object:
const initialState = 0;
const reducer = {
  increment: (count, { payload }) => count + payload,
  decrement: (count, { payload }) => count - payload
};

const [state, actions] = useReduction(initialState, reducer);

The hook returns the current state and an object with action creators. When an action creator is called, an action ({ type, payload }) will be dispatched to update your state.

Actions

Action names are derived from reducer names to minimize boilerplate. Providing useReduction with a reducer increment() will generate an action creator also named increment() that dispatches an action with a type “increment” and uses the first argument as payload. increment(1) would use 1 as the payload provided to the reducer.

// use actions to update count from previous example
actions.increment(1)
// count = 1
actions.decrement(2)
// count = -1

Debugging

Pass true as the third argument in useReduction to enable debug mode. It will log the dispatched action to the console.

const [state, actions] = useReduction(initialState, reducerMap, true);
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].