DEV Community

What Even Is A Dispatch Function?

Dustin Myers on February 15, 2020

Learning redux? Or useReducer? Then chances are you've been frustrated by the black magic that is the dispatch function 🧙‍♂️! Well, luckily you hav...
Collapse
 
habibaman766 profile image
Habib Ur Rehman

The disptach function calls a reducer function, but i came to know that the control flows in this way.
dispatch -> store -> combineReducer -> reducer
I am not understanding that disptach function is calling the exact reducer function but why the the data is passed to store first then to combineReducer and then to reducer.\
Please can you explain this

Collapse
 
kelviniot profile image
KelvinIOT • Edited

Hello Habib,

  • So i understand that Store holds the whole state tree of your application.
  • Then you pass to the store, your combineReducer which combines all your separate reducers in an object.
  • When the dispatch function calls the store, it actually tries all your Reducers.
  • But the Reducers listen for events checking for a matching action-type, so if the action-type passed to the dispatch finds a match in a reducer, that particular reducer runs.
Collapse
 
dustinmyers profile image
Dustin Myers

This is absolutely right! Sorry I was late on the reply. Hopefully kelvin's response helped you understand how this system works with combined reducers. Feel free to reach out if you have any other questions! And thank you Kelvin. That was a great explanation 👍👍

Collapse
 
habibaman766 profile image
Habib Ur Rehman

Thank you very much for understanding.

Collapse
 
keitharnold profile image
KeithArnold

Thank you Dustin, I've been spinning my wheels for more than a week to understand useReducer and at last it makes sense!

Collapse
 
dustinmyers profile image
Dustin Myers

Seriously the best kind of reply! Thank you, and I'm glad it was helpful!

Collapse
 
zallex profile image
Zalyotov Alexey

Thanks a lot! It's pretty clear and helpful

Collapse
 
dustinmyers profile image
Dustin Myers

Thank you! I'm really glad this helped!

Collapse
 
johnkeers profile image
John Keers • Edited

If you have multiple reducers, can you tell me how the dispatch function knows which reducer to call?
Is there a switch statement internally with action type as the parameter?

Update: Looking at the source code for createStore it looks like it only ever takes a single reducer? Does that mean you simply use combineReducer() and createStore will choose the correct reducer to use?

Collapse
 
dustinmyers profile image
Dustin Myers • Edited

Actually when you dispatch an action, it will go through all your reducers that you've "combined". It's the action type that's the magic here. You probably only have one reducer that handles that specific action type. All the other reducers will look at the action type, and return their slice of the state untouched.

Collapse
 
gaddopur profile image
gaddopur

Thank you very much. It helped very much. Before reading this I was in confusion but you clear all of my doubts once again thank you very much.

Collapse
 
dustinmyers profile image
Dustin Myers

Awesome!! So glad it helped 😊

Collapse
 
sodj profile image
Sodj • Edited

I came here trying to understand why the actionCreator function can't call the dispatch directly ... why do we have to do dispatch(actionCreator()); instead of just actionCreator();

Collapse
 
rarrrleslie profile image
Leslie Rodriguez

I tried, but I could not get away from Redux. So, thanks for this amazingly simple refresher! :)

Collapse
 
dustinmyers profile image
Dustin Myers

Thanks Leslie! 😁

Collapse
 
ashutoshjs profile image
AshutoshJs

it helped :)

Collapse
 
dustinmyers profile image
Dustin Myers

Yay!! That makes me so happy!

Collapse
 
calebtripp profile image
Caleb Tripp

Thank you. I'm brand new to React and this is helping me grok dispatch and reducers.