hub-flow
Immutable state management powered by Freezer-JS, targeted towards React
Hub Flow
Hub Flow is a pattern for managing state in React.js applications. It steals the great principles from Flux & Redux (one-way data flow), but leaves out the ceremonious complexity. Hub Flow comes with built in immutability, which means a huge performance boost and an end to accidental state mutation bugs. Hub Flow is powered by Freezer.js
npm install --save hub-flow
Getting Started
hub.ts
;; // Describe the Shape of your App State // Setup the default AppState// Unless you specify, the app state will be loaded// from cache if possible; // Create the HUB; ;
Once you setup a hub, you want to create a Model
for the various slices/layers of your application state. In the Model
you can
- create methods that update AppState
- Setup specific listeners so that your component only re-renders when it needs to.
- Transform data / setup computed values
- Setup relationships with other layers of the application (optionally)
counter.models.ts
;;
Then use it in a React component by leveraging useModel
.
import React from "react";import Counter from "models";import useModel from "hub-flow"; { console; const counter = ; return <div>Counter: countercount</div>;} { console; const counter = ; return <div> <button =>Reset</button> <button =>-1</button> <button =>+1</button> </div> ;}
Under the covers, useModel
hook:
- Wraps your Model's contstructor witha React.useMemo so that you don't get a new instance of your model with each render
- Additional params will be passed to your Model's constructor
- Wires up to the listeners that you defined in your
Model
so that React renders any time that listener receives andupdate
event.
useModelCounter new Counter;// Additional params will be passed to your Model's constructoruseModelUser, 3 new User3;