3.0.0 • Published 6 years ago

react-callbag v3.0.0

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

Asynchronous reducer pipelines using callbags.

Try it now on CodeSandbox.

Install

npm install react-callbag --save

Pipeline operator

If you don't have the pipeline operator you can use the pipe function. foo |> bar() would instead be pipe(foo, bar()).

Basic usage

import { Subject, reducerFromMap, startWith } from "react-callbag";

const reducers = new Map([
  ["SUBTRACT", (state, amount) => ({ count: state.count - amount })],
  ["ADD", (state, amount) => ({ count: state.count + amount })],
  ["MULTIPLY", (state, multiplier) => ({ count: state.count * multiplier })]
]);

const pipeline = actions =>
  actions |> reducerFromMap(reducers) |> startWith({ count: 0 });
<Subject pipeline={pipeline}>
  {(state, send) => (
    <div>
      <button onClick={() => send("SUBTRACT", 1)}>Remove 1</button>
      <button onClick={() => send("ADD", 1)}>Add 1</button>
      <button onClick={() => send("MULTIPLY", 2)}>Multiply by 2</button>
      <div>{state.count}</div>
    </div>
  )}
</Subject>

Debouncing example

import { debounce } from "callbag-debounce";

const pipeline = actions =>
  actions
  |> debounce(250)
  |> reducerFromMap(reducers)
  |> startWith({ counter: 1 });

Further reading

3.0.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

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