react-state-modules

1.2.0 • Public • Published

React State Modules

React State Modules mmakes it simple to connect your React Components to any of your State Modules easily. It provides a convenient mechanism that is meant to work similar to the react-redux connection pattern. state-modules is an Application State Manager which produces similar effects to using redux, redux-saga, reselect, seamless-immutable, and redux-saga-process. However, it is packaged into a very small package with no dependencies (other than those written by and for this project).

// store.js
import createState from "state-modules";
import connectReact from "react-state-modules";
 
const state = createState().component({
  config: { cid: "counter" },
  state: {
    counter: { value: 0 }
  },
  actions: {
    increment: ["by"],
    decrement: ["by"]
  },
  reducers: {
    INCREMENT({ by = 1 }, draft) {
      draft.counter.value += by;
    },
    DECREMENT({ by = 1 }, draft) {
      draft.counter.value -= by;
    }
  }
});
 
const connector = state.connect(connectReact);
 
export default connector;
// app.js
import React, { Component } from "react";
 
import connect from "./store";
 
class App extends Component {
  render() {
    const { value, increment, decrement } = this.props;
    return (
      <div>
        <p>Current Value: {value}</p>
        <button onClick={() => increment(1)}>Increment</button>
        <button onClick={() => decrement(1)}>Decrement</button>
      </div>
    );
  }
}
 
export default connect(
  () => ({ value: "counter.value" }),
  actions => actions
)(App);

Package Sidebar

Install

npm i react-state-modules

Weekly Downloads

18

Version

1.2.0

License

MIT

Unpacked Size

39.7 kB

Total Files

13

Last publish

Collaborators

  • bradynapier