🗻 Redux CBD
Typescript decorators\annotations, utils and abstract classes for react-redux application.
Adds various utility annotations such as @Single, @EntryPoint, @Connect or @Wrapped.
Allows you to write class-based declarations of your data storage with strict and predictive typing.
Enforces typesafety and OOP mixed with functional style (all key features and implementation of redux remains the same).
Intended to be used with react-redux.
Warning
You are viewing old version of package without separation.
For newest releases check '@redux-cbd/core'.
Installation
npm install --save redux-cbd
Important:
- Package uses proposal ES reflect-metadata api, so I would advice to get acknowledged with its usage.
- Package uses 'expirementalDecorators' features (disabled by default for TypeScript transpiler).
Setup
1) Install package.
2) Inject 'reflect-metadata' into your bundle (webpack entry or import inside your entryfile).
3) Configure typescript. You should turn on "emitDecoratorMetadata" and "experimentalDecorators" for compiler.
4) Create some actions (extend simple, complex, async) with @ActionWired annotation.
5) Create related reducer(extend ReflectiveReducer) with proper @ActionHandlers.
6) Create rootReducer that includes reflectiveReducers. Declare storeState interface.
7) Create store based on root reducer. Extend CBDStoreManager, annotate @StoreManaged. Include cbdMiddleware there.
8) Create @StoreConnect decorator.
9) Connect component => use props and actions from declarative storage.
tsconfig.json part:
What is inside
Example (wiki contains more explanations):
Application entrypoint:
;;; ;;
Store, provider and connect creations:
;; /* Global store state typing, includes reducers for this one (can exist multiple stores in our app). */;/* Singleton store manager. Creates store, providers, contains some info about store. */;/* Global store provider wrapper, provides correct store and store key for connection. No need to manage store manually. */;/* @Connect decorator annotation linked to global store, components can be wrapped in multiple connects with different stores. */;
State declarations:
/* State for demo reducer store. *//* Class over interface for default init. Will transform to simple object after redux processing. */ /* State for global store. */
Our demo reducer:
; ;; // Reducer class. Typing allows you to create ONLY methods with two params - <genericState, actionType>.// Looks for method with same action type and executes it. Just like functional reducer with switch but better.// @ActionHandler is not required. Method name does not influence on behaviour.// Same action handlers are not allowed inside one class.
Our actions for reducer methods (considered to be separate class-files, you know):
;
Global store manager:
;; /* Custom middlewares. */; /* Store state, that includes smaller reducers. */; /* Some Reducers declaration. */;;
Connected component
;;;; // Store related things.;; // Props, that are injected from connect store. // Props, mapped and injected as actions creators. // Own props, that are passed with manual component/container creations.// Router-managed components are not so complicated because we don't create them manually. // External props, that are injected by different decorators.// For example: @Connect, @withStyles (material ui), @withWrapper (provide some props with HOC by decorator usage) etc. // General props for whole component for overall picture, everything can be accessed from the inside. // Link global store provider with component. This props will be injected automatically and should be type safe.
Documentation:
Repository wiki includes doc and samples.
Proposals and contribution:
Feel free to contibute or mail me with questions/proposals/issues (Neloreck@gmail.com).
Full examples
Repository includes example project with commentaries: link.
My own 'redux-cbd' based project: link.
Library unit tests also include some different examples of cbd usage: link.
Licence
MIT