Answer the question
In order to leave comments, you need to log in
What is the point of using actions, actionCreators etc. in react-redux?
The generally accepted approach implies that if I want to change something in the store state, I have to do 4 actions:
1) Create a constant for the action (it is naturally inconvenient to send and process strings)
2) Create an action creator
3) Create a function for mapDispatchToProps in each component , which calls the creator
4) Write the state change logic in the reducer
Why is everything so complicated if in the vast majority of cases (in my experience) it is still one type, one creator and one logic for processing it in the reducer? Why not simplify? For example, we create an actions.js file (Or many files with a logical breakdown), in which we write functions that immediately change the state
// actions.js
export const setCategories = (payload, setState, currentState) => setState({ categories: payload })
export const addCategory = (payload, setState, currentState) => setState({ categories: [...currentState.category, payload] })
// actionsDispatcher.js
import * as actions from 'actions'
const getActions = (dispatch, ownProps) => {
Object.keys(actions).forEach(actionName => {
const fn = actions[actionName]
actions[actionName] = payload => dispatch({ action: fn, payload, type: _.toSnakeCase(actionName) })
}
return actions
}
const mapDispatchToProps = getActions
function rootReducer(state = initialState, action) {
if (typeof action.action === 'function') {
return action.action(action.payload, setState, state)
} else if (action.type === '...') {
// здесь обычный подход
}
}
Answer the question
In order to leave comments, you need to log in
Bare Redux is a low-level constructor and set of guidelines. Any project that uses redux sooner or later starts using self-written or third-party (like redux-toolkit) wrapper libraries over redux in order to reduce the number of boilerplate and make life easier for itself.
If you understand and accept your approach, whatever it may be, with all its pluses and minuses, and it works for you, use it to your health. It may not suit others.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question