Redux Fundamentals (feat. React)

Creating a Slice of State

Steve Kinney

Steve Kinney

Redux Fundamentals (feat. React)

Check out a free preview of the full Redux Fundamentals (feat. React) course

The "Creating a Slice of State" Lesson is part of the full, Redux Fundamentals (feat. React) course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates how to use Redux Toolkit to create a slice of state using configureStore to apply a few starting middleware and connect the developer tools. The createSlice ability creates a slice of state to assist with state management.


Transcript from the "Creating a Slice of State" Lesson

>> So we're gonna go. If you go into the repose in the, on the course website, there's one, called chores Redux, which is a relatively, simple app. And what it seeks to do is maintain a list of humans, maintain a list of tasks and give you eventually the ability to assign tasks to a human.

So I can see who has to do what, around the house. So let's go ahead and we know that we're gonna need two slices of state. We're gonna need one for the humans and one for the tasks. And then we'll begin to use both of them to kind of build our application up.

So let's go into this code base. And we'll switch out to, I got to open this one up. That's the wrong one. I just. All right, give it one second we've got open in Visual Studio. So we've got our components. Our store right now is actually the normal one from react Redux.

So we wanna actually, instead of using Redux directly, we're gonna use Redux toolkit for most of what we need. So we'll get rid of this code and we're gonna export our conts store is configure store which is an abstraction that will eventually take some options and return a fully configured store things that are gonna happen in here.

It will automatically apply a few different middlewares for us. In development there are three middlewares there is one that checks if you're accidentally mutating state somewhere and yells at you about it. Another that makes sure we saw that you can export an import entire serialized version of state as JSON that works with our objects and arrays if you start using maps and sets stuff starts to get weird.

So it will go and check to make sure you're not doing that. And then it will give you a Redux stunk, which we briefly mentioned earlier, which allows you to dispatch functions, to make Ajax calls. It's gonna wire up all that middleware for you. It's gonna wire up the developer tools.

It's gonna do everything under the hood for you. Get you kind of set up. So it takes a reducer key. We'll just start out with our simple one for now, but we'll add to it momentarily, but we know that things get angry with us if we don't have it in place.

So now if we go over here, and just with that configure store We can see that we already have the dev tools in place. Great, so let's create the tasksSlice. So I'll go in this store. TaskSlice.js. And we need this createSlice ability. So we'll go ahead and we'll start with, we'll just say.

CreateSlice, and there's a bunch of other little helpers in here. Before we were just incrementing an ID. But Redux toolkit also comes with a little helper library called nano ID, which just create unique IDs for you. So we'll make a function. This is a regular JavaScript function for creating a task.

And really, the only thing that we're going to need eventually is a title for this task. And the ID is gonna use this library called nanoid. We'll get the title. We'll say, completed as false in the very beginning. This is very similar to what we did with the menu items.

And assigned to, will be an empty string for now. Great, so we've got that initial state. We'll just get some of that in place. And we'll say createTask. Do order more energy drinks again. And make another one, I don't know. What are the plants? Right, so this is again just calling this function.

We did it by hand in the previous ones. I've just given myself a little helper. And now we just need to make the slice. So we can say, export const tasksSlice is createSlice. And we know that the name is going to be tasks. We know the initial state, we have here.

And then we have our reducers. Right? These are all the things that can happen. Let's just start with the simplest thing which is we know we saw that form we know we're gonna need to add one. So we'll say add and just like before, it has the same API as any reducer itself will say state and action.

And then const task is that createTask helper that we made before. And whatever the action dot payload is. And then really we just push that onto the state. I could even get this down to a one liner if I really wanted to. And we've got that in place.

So now let's just hook it up to the, to the store, which we'll get rid of this one here. And let's go ahead and we'll just say. Do you remember the syntax for combined reducers where we have keys and then we have the value is a reducer? That's exactly the same syntax.

It's using everything we've seen so far under the hood to wire this all together for us. So we'll say tasks and that's gonna be tasksSlice.reducer. And great, so we have all of that in place. Let's go take a look in our developer tools and see if that's in our state now.

TasksSlice is not meant corresponding on disk, I don't know why, we capitalize that with the auto complete but here we are. Can I read probably map undefined Okay, we don't have the humans yet because I move that out of there. So actually say I should do the trick.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now