Reducers in Redux are primarily designed to be pure functions that take the previous state and an action as arguments, and return a new state object without mutating the previous state. However, it is still possible for developers to directly mutate the state, going against Redux's philosophy.
const reducer = (oldState = initialState, action) => {
switch (action.type) {
case "ACT": {
// mutate state directly instead of creating a new one
oldState.subobj.ssub.key = "hi hi";
return oldState;
} default: {
return oldState;
}
}
};
This raises the question of why Redux does not perform verification checks to prevent such actions. How can we ensure that developers adhere to Redux principles and avoid direct state mutations?