Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Redux: Implementing store from scratch

In redux, store is a very cen­tral piece which brings actions, reduc­ers and states together. It has the fol­low­ing main func­tions and API :

  • Holds appli­ca­tion state;
  • Allows access to state via getState();
  • Allows state to be updated via dispatch(action);
  • Reg­is­ters lis­ten­ers via subscribe(listener);
  • Han­dles unreg­is­ter­ing of lis­ten­ers via the func­tion returned by subscribe(listener);.

Redux Store


Note:

The sub­scribe func­tion returns another func­tion, which fil­ters out the lis­tener from the list of lis­ten­ers. Hence, if you want to unsub­scribe a lis­tener then you will have to save the ref­er­ence for the return value while sub­scrib­ing and call the ref­er­ence function.

For exam­ple,


let foo = () => {...}; let unsubscribeFoo = store.subscribe(foo); // At this point foo will be called whenever the store updates // sometime in future unsubscribeFoo(); // After this function is executed, the foo will not be called whenever the store updates