I am new to react/redux. I am trying to figure out how all the pieces in redux interact. The one thing giving me trouble is understanding the relation between actions and reducers. When an action is called, how does the store know which reducer to use? Does it base it completely on the action type name? Do type names have to be unique? To whom or what does the reducer pass the new state object to, the store or the action?
As I understand it, it goes like this:
Reducers, as the name suggests, take in two things: previous state and an action. Then they reduce it (read it return) to one entity: the new updated instance of state. So reducers are basically pure JS functions which take in the previous state and an action and return the newly updated state.
Reducer - is what manipulates that data when it recieves an action. Action - is what tells reducer to manipulate the store data, it carries the name and (not required) some data.
Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.
If the application's state is managed by Redux, the changes happen inside a reducer function — this is the only place where state changes happen. The reducer function makes use of the initial state of the application and something called action, to determine what the new state will look like.
In a typical Redux setup, the actions are dispatched to ALL reducers and it's up to the reducers to decide if they care about that action. A common pattern is a switch in the reducer that checks action.type, has cases for actions it cares about and a default case that just returns the current state like this:
export default (state = false, action) => {
switch (action.type) {
case START_LOADING:
return true;
case STOP_LOADING:
return false;
default:
return state;
}
}
In this case, I am telling my reducer it only cares about the actions with type START_LOADING
or STOP_LOADING
and that in all other cases it should just return it's previous state.
For a good understanding of Redux (and Flux) I'd suggest you check out Code Cartoons by Lin Clark or her video which covers most of the same things.
1: how does the store know which reducer to use -> This is based entirely on the action type.
2: Do type names have to be unique? -> This is not a rule. But mostly, yes. Each action has a distinct type name and the corresponding reducer gets invoked.
3: To whom or what does the reducer pass the new state object to, the store or the action? -> The reducer does not pass the new state object anywhere. Basically, it triggers a state change event to all your react components that are listening to it. All components listening to the changed state get re-rendered, with the new version of the state, thereby updating your DOM.
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