0.1.4 • Published 4 years ago

redux-describe-state v0.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

redux-describe-state

Yet another abstraction over redux actions

Installation

npm i redux-describe-state

Usage

import { createStore, combineReducers } from "redux";
import describeState from "redux-describe-state";

let { reducer: todosReducer, buildAction } = describeState({
  name: "todos",
  getInitialState: () => []
});

function addTodo(todo) {
  return buildAction(todos => [...todos, todo]);
}

function removeTodo(todo) {
  return buildAction(todos => todos.filter(t => t !== todo));
}

function resetTodos() {
  return buildAction(() => []);
}

let store = createStore(combineReducers({todos: todosReducer}));
let getTodos = () => store.getState().todos;

console.log(getTodos()); // => []

store.dispatch(addTodo('do this'));
console.log(getTodos()); // => ['do this']

store.dispatch(addTodo('do that'));
console.log(getTodos()); // => ['do this', 'do that']

store.dispatch(removeTodo('do this'));
console.log(getTodos()); // => ['do that']

store.dispatch(resetTodos());
console.log(getTodos()); // => []

API

describeState

describeState({name, getInitialState})
  • name: name for this state chunk. Used in generating action name. Required.
  • getInitialState: a function which will be called in the reducer if state is blank. Required.

Returns and object with following keys:

  • reducer: a reducer function which can be passed to redux's combineReducers or used on its own.
  • buildAction: action builder function.
let action = buildAction(currentState => nextState);
dispatch(action);

License

MIT

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago