Skip to content

HamadTheIronside/redux-store-subscribe

Repository files navigation

Redux Store Subscribe Middleware

Using the Redux Store Subscribe Middleware, we can subscribes to value changes in the store.

How to install?

npm install --save redux-store-subscribe

How to use?

Source Code

store.js

import { configureStore } from "@reduxjs/toolkit";
import { storeSubScribeMiddleWare } from "redux-store-subscribe";
import { exampleReducer } from "./slice";

export const store = configureStore({
  reducer: {
    exampleReducer,
  },
  middleware: [storeSubScribeMiddleWare()],
});

AnyWhere.js

import { useEffect } from "react";
import { subScribeToStore, unSubScribeFromStore } from "redux-store-subscribe";

useEffect(() => {
  const id1 = subScribeToStore(
    "exampleReducer.count1",
    ({ prevValue, nextValue }) => {
      alert(`count1 changed from ${prevValue} to ${nextValue}`);
    }
  );

  const id2 = subScribeToStore(
    "exampleReducer.count2",
    ({ prevValue, nextValue }) => {
      alert(`count2 changed from ${prevValue} to ${nextValue}`);
    }
  );

  return () => {
    unSubScribeFromStore(id1);
    unSubScribeFromStore(id2);
  };
}, []);

How to use with TypeScript?

...

Roadmap

  • Providing More Examples
  • Providing TypeScript Examples
  • Writing Tests

About

A middleware that provides subscribes feature on value change.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published