You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importcreateSagaMiddlewarefrom'redux-saga';import{createStore,applyMiddleware,combineReducers}from'redux';import{reducerascontrollerReducer,sagasascontrollerSaga}from'redux-saga-controller';// NOTE Build the middleware to run our SagaconstsagaMiddleware=createSagaMiddleware();// NOTE Create store outside of root to be able dispatch actions from anywhere!conststore=createStore(combineReducers({// ...your other reducers here// you have to pass controllerReducer under 'controller' key,controller: controllerReducer,}),applyMiddleware(sagaMiddleware));// NOTE Initialize application sagassagaMiddleware.run(controllerSaga);exportdefaultstore;
Step 2: Prepare controller annotation
importControllerfrom'redux-saga-controller';// NOTE Initial data for your redux stateconstinitial={initialized: false,disabled: false,data: {name: 'John',age: 30,}};// NOTE Create Controllerexportconstcontroller=newController({DEBUG: true,// Enable DEBUG Mode
initial,// Setup initial data for redux stateprefix: 'root',// Controller nametypes: ['INITIALIZE','GET_SELF'],// Types for which action creators will be generatedsubscriber: function*(){yieldtakeEvery(controller.TYPE.INITIALIZE,initializeSaga);}});exportdefaultcontroller;function*initializeSaga({ type, payload }){console.log(`%c ${type} `,'color: #FF6766; font-weight: bolder; font-size: 12px;','\n payload:',payload);yieldput(controller.action.GET_SELF());yieldput(controller.action.UPDATE_CTRL({initialized: true}));}
Step 3: Use it inside your react components
importReactfrom'react';import{useController}from'redux-saga-controller';import{controller}from'./controller';exportconstExample1=memo(()=>{// NOTE Prefer wayconst[{ data, disabled, initialized },{INITIALIZE,CLEAR_CTRL,GET_SELF},isControllerConnected]=useController(controller);useEffect(()=>{INITIALIZE();returnCLEAR_CTRL;},[INITIALIZE,CLEAR_CTRL]);if(!initialized||!isControllerConnected){returnnull;}return<div><h1>Hello {data.name}! Your age is {data.age}</h1><buttondisabled={disabled}onClick={()=>GET_SELF()}>Get Details</button></div>;});
API
To setup controller you need
Field
Type
Require/Optional
Default value
Description
DEBUG
boolean
optional
false
In DEBUG mode you will get additional information in console
prefix
string
optional
controller_${unique_index}
The unique name of controller and field name in the store
initial
object
optional
{}
Initial data of your store
types
Array[string]
optional
[]
All list types which you need (Actions for these types will be generated automatically)
subscriber
Generator
required
undefined
Redux-saga subscriber
importControllerfrom'redux-saga-controller';exportconstcontroller=newController({DEBUG: true,// Enable DEBUG Modeinitial: {},// Setup initial data for redux stateprefix: 'my-controller',// Controller nametypes: ['ACTION_1','ACTION_2'],// Types for which action creators will be generatedsubscriber: function*(){}});
Already created controller contains
Field
Type
Description
DEBUG
boolean
In DEBUG mode you will get additional information in console
initial
I
Initial data of your store
name
string
The unique name of controller and field name in the store
TYPE
Record<T, string>
All list types which you passed
selector
ControllerState
Selector function which will return all controller state
selectorInitial
state => I
Selector function which will return initial data
selectorActual
state => I
Selector function which will return actual data
selectorConnected
state => boolean
Selector function which will return isControllerConnected
subscriber
Generator<ForkEffect, void, unknown>;
Generator function for redux-saga
action
Record<T | DefaultActions, ActionCreator>
All available action creators
React hooks
// useController - to use you controller and you will get all data you needconst[reducer,actions,isControllerSubscribed]=useController(controller);// To get separately you can use next hooksconstreducer=useControllerData(controller);constactions=useControllerActions(controller);constisControllerConnected=useControllerSubscribe(controller);