I have spent about two hours trying to trace and fully understand the inner workings of createStore() when it calls a Reducer. Specifically, how does the Reducer keep track of the current state? I am at a point in my school lesson where I am starting to learn about how to dispatch an action from a component and update the store. Well, in the default code that was given to me that function was in the store.js file, which has a callback calling the Reducer, and even though the app was working I didn’t know why it was working. How was the Reducer using the most up-to-date app state? Was the createStore function sending the state to the Reducer?
I found my answer in the Redux documentation, but that isn’t the point of this blog post. It got me thinking about something. Sometimes I feel like I ask my mentor and my fellow classmates weird abstract questions that nobody else is asking. Usually technical stuff about why things work. Like, why and how does createStore() work, for example. Sometimes these things just work, like magic. It makes me feel like they are grasping the concepts quicker than I am, but I was thinking more about that tonight and came to a different conclusion.
I think a lot of aspiring developers, and even accomplished developers, are ok with just letting the magic happen; accepting that some of the things just … work. I asked my mentor about the Reducer and how it gets the state. He said that the initialState was getting updated.
It sounded good. It made sense. I mean, I was setting state=initialState after all. So we signed out and I went on to practice some more. I decided to log the initialState at several different points in the Reducer, and guess what. It stayed the same every time! The initialState was not being updated. My mentor was … wrong? How could it be.
Now that I’ve done this research I probably won’t ever forget how that specific part of Redux works. Why are others ok with the magic? I’m way too curious to settle for that! Plus, if I actually understand it then I don’t need to rely on my memory. If I get stuck, I just work through it step by step and as long as I understand it then I can figure it out. I think they call that troubleshooting ; )
I’ll leave you with this fun interesting video: how did Jordan Walke come up with React?