I do not think that you should listen to the changes directly from the store, but you will need it to send actions.
Listening to changes
In redux natural place where you could name such logic would be from action , which sends the change to the field.
export function updateSomeField(value, field) { fieldValidator.validate() return { type: ActionTypes.UPDATE_SOME_FIELD, field: field, value: value } }
fieldValidator can contain a reference to the store to get the state it needs, to execute its logic using store.getState() , or to get this data from the action as a parameter (using asynchronous actions)
export function updateSomeField(value, field) { return (dispatch, getState) => { fieldValidator.validate(getState.myScreen.fields) dispatch({ type: ActionTypes.UPDATE_SOME_FIELD, field: field, value: value }) } }
Retrieving Storage
Once you create it, you can simply provide it to everyone who needs it. You can do this by initializing singleton
let store = createStore(); FieldValidator.setInstance(new FieldValidator(store));
Or by typing a member
let store = createStore(); fieldValidator.store = store;
Moti azu
source share