1.0.6 • Published 6 years ago

redux-functional v1.0.6

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

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
1.0.6

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago