Last active
August 10, 2024 10:14
-
-
Save ibrkhalil/d2852ba30d576e64297abdf2e42e0371 to your computer and use it in GitHub Desktop.
Redux state update logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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