I think the Redux holds a lot of great value however the main issue for me lies with how reducers are written today:
const addToDoReducer = (state, action) => {
switch (action.type) {
case ADD_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action)
})
case TOGGLE_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action)
})
default:
return state
}
}
... this at the end reducers become the weak/bridle spot of the project
Q: Is there a better way/option to write reducer that:
something more like this:
const addToDoReducer = (state:toDoState, action:addAction) =>
{
return { ...state, toDos: [...state.toDos, action.toDoObject] };
}
Or is there a library that does this already?
A Redux app really only has one reducer function: the "root reducer" function that you will pass to createStore later on. That one root reducer function is responsible for handling all of the actions that are dispatched, and calculating what the entire new state result should be every time.
While it's certainly possible for each of these to become a performance concern in sufficiently complex situations, there's nothing inherently slow or inefficient about how Redux is implemented.
Reducers are the only way to change states in Redux. It is the only place where you can write logic and calculations. Reducer function will accept the previous state of app and action being dispatched, calculate the next state and returns the new object.
Pure reducers have no side effects and enable things like time-travelling. They make reasoning about application behavior easier.
Please read the "Reducing Boilerplate" docs page for examples of how you can write your own reducer utilities, such as a function that takes a lookup table of action types to specific reducer functions for each type. Specifically:
function createReducer(initialState, handlers) {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action)
} else {
return state
}
}
}
The "Structuring Reducers" docs section also gives instructions and ideas on ways to organize your reducer logic.
For further information, please see:
Remember: Redux calls your root reducer function, which is code that you have written. How that reducer does its work is your choice. If you don't like switch statements, don't write them. If you find the code repetitive, write factory functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With