Check out a free preview of the full Advanced State Management in React (feat. Redux and MobX) course:
The "Redux Store: createStore" Lesson is part of the full, Advanced State Management in React (feat. Redux and MobX) course featured in this preview video. Here's what you'd learn in this lesson:

Create a new store and a subscriber that listens when actions are dispatched to the store.

Get Unlimited Access Now

Transcript from the "Redux Store: createStore" Lesson

[00:00:02]
>> Steve Kinney: So we have this Redux Store. We can now produce new states of the world. We need to store that somewhere, right? I'm trying to think of the right now, and if I looked up here for where I could store stuff, I'll create a place to store stuff.

[00:00:16] Well, I could use Create Store. That seems good. So I'll go ahead and I'm just gonna use it, we'll find out what happens. If I just call createStore, I'm not gonna like blow up my console cuz I know what's gonna happen. This is gonna blow up. One does not just create a store, you also have to give it a reducer.

[00:00:39] But turns out, guess what I have? I made a reducer just before.
>> Steve Kinney: So I hand it this reducer, I save it.
>> Steve Kinney: I've got the store. And it's a lot for the console right here, but it itself only has a few methods, right? It's got a dispatch method, it's got a getState method, there's not a lot necessarily going on with this one either.

[00:01:09]
>> Steve Kinney: So let's play around with some of these. We could, for instance, let's go ahead and let's create a subscriber, lets subscribe to the story cause like your state changes. We can imagine a world where the view layer might want to know that the state of the world changed, and listen for it, and receive that new state of the world and update the view.

[00:01:33] That seems rational, right? So let's take a look at that. Again this is, Redux doesn't have anything to do with reacts, we're just imagineering right now. Cool, so we'll just have a console log that's gonna be like subscription. And, we'll actually say, we're passing the store and we'll say getState.

[00:02:07]
>> Steve Kinney: And I'm just going to get the result off of that, because I don't want to blow up this console. So, we have this subscriber, which is just a function, that console logs, and will at any given point log out the state of the store. Very cool, and the next thing we need to do is we have the store and it has a subscribe method.

[00:02:29] I will give it subscriber. That will return a value, which will be a function that you can call to unsubscribe. So I could say
>> Steve Kinney: And then if I called unsubscribe later, that event scenario would be taken off. All right, let's try it out.
>> Steve Kinney: We've got the store.

[00:02:57] And let's store.dispatch which is our next method to play around with. We'll say store.dispatch and we'll give it the
>> Steve Kinney: Add action, and you can see that the subscription rang true and the current state was 4. Let's call it again. Look. The current state is now eight, right?

[00:03:28] So, the store is keeping track of the current state of the world. It's receiving, it's dispatching actions to go into the reducer, which is then modifying the state of the world and it's keeping track of that, right? So all of a sudden, we have a way to manage state, we have a way to manipulate state.

[00:03:47] Very cool. Now I could implement subtract, and multiply, and all those, but I'm gonna to spare on that right now because it's basically means using every operator on the number of my keyboard, and that in variants different factions. So not necessarily gonna do that right now.