/context-observable

Use same interfaces found in Redux and Redux-Observable to manages app state via new React Context Api

Primary LanguageJavaScript

Context-Observable

Travis npm package Coverage Status

Context-Observable Component manages the state of your React App using new React Context Api following same interfaces of Redux (actions and reducers) and using the same idea behind redux-observable to avoid "side-effect".

The Context-Observable Component

Look how it's simple

Dependencies

Install

Via npm

npm install --save context-observable

Via UMD (to use as jsbin, etc..)

https://raw.githubusercontent.com/stvkoch/context-observable/master/umd/context-observable.js

Run Demo

git clone git@github.com:stvkoch/context-observable.git
cd context-observable
npm start

What you need to manages the state of your app?

As a redux, you will need create reducers and actions.

What you need to avoid "side-effect"?

Context-Observable follow same ideia behind redux-observable, so what you will need is create your epics the same way that you already do in redux-observable.

How I setup Context-Observable?

Then you import Context-Observable Component and pass your epics and reducers.

import { ContextObservable } from "context-observable";

import rootEpics as epics from "./epics";
import rootReducers as reducer from "./reducers";

const App = () => (
  <ContextObservable {...{ epics, reducer }}>
    <Product />
  </ContextObservable>
);

How you dispatch actions to epics and to reducers?

import {Consumer} from 'context-observable';

  // ... in your render method:
  <Consumer>
    {({ state, dispatch }) => (
      <React.Fragment>
        <ul>
          {state &&
            state.products.map(p => (
              <li key={p.id}>
                <button onClick={() => dispatch(addProduct(p))}>
                  {p.name}
                </button>
              </li>
            ))}
        </ul>
      </React.Fragment>
    )}
  </Consumer>