Skip to content

philipyoungg/redux-functional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Functional Redux

Create a reducer in functional way @param {any} - initial state could be anything @param {Object} - an object with ACTION_TYPE on key and function on the value. Each function are passed the action object and state to the argument.

import createReducer from "redux-functional";

// helpers
const addOne = (action, state) => state + 1;
const removeOne = (action, state) => state - 1;
const alwaysZero = () => 0;
const identity = action => action.payload;

const initialState = 0;

const handlers = {
    INCREMENT: addOne,
    DECREMENT: removeOne,
    RESET: alwaysZero,
    SET_VALUE: identity,
};

const counter = createReducer(initialState, handlers);

// later

const store = createStore(counter);

const incrementCounter = { type: "INCREMENT" };
const decrementCounter = { type: "DECREMENT" };
const resetCounter = { type: "RESET" };
const setCounterValue = payload => {
    type: "RESET", payload;
};

store.dispatch(incrementCounter); // state = 1
store.dispatch(incrementCounter); // state = 2
store.dispatch(incrementCounter); // state = 3
store.dispatch(decrementCounter); // state = 2
store.dispatch(resetCounter); // state = 0
store.dispatch(setCounterValue(7)); // state = 7

About

Create redux reducer that supports functional composition.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published