Skip to content

Instantly share code, notes, and snippets.

@ibrkhalil
Last active August 10, 2024 10:14
Show Gist options
  • Save ibrkhalil/d2852ba30d576e64297abdf2e42e0371 to your computer and use it in GitHub Desktop.
Save ibrkhalil/d2852ba30d576e64297abdf2e42e0371 to your computer and use it in GitHub Desktop.
Redux state update logger
import { createStore } from "redux";
const monitorStateUpdateEnhancer =
(createStore) => (reducer, initialState, enhancer) => {
const monitoredReducer = (state, action) => {
console.log("STATE BEFORE => " + state.value);
const newState = reducer(state, action);
console.log("STATE AFTER => " + state.value);
return newState;
};
return createStore(monitoredReducer, initialState, enhancer);
};
const incrementAction = {
type: "increment",
};
const reducer = (state = { value: 0 }, action) => {
if (action.type == incrementAction.type) {
state = { value: ++state.value };
}
return state;
};
const initialState = {
value: 0,
};
const store = createStore(reducer, initialState, monitorStateUpdateEnhancer);
var i = 5;
while (i--) {
store.dispatch({ type: "increment" });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment