Using the Redux Store Subscribe Middleware, we can subscribes to value changes in the store.
npm install --save redux-store-subscribe
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);
};
}, []);
...
- Providing More Examples
- Providing TypeScript Examples
- Writing Tests