Zustand Crash Course — Transcript

Learn how to use Zustand for efficient state management in React apps, replacing context with a simpler, scalable solution.

Key Takeaways

  • Zustand provides a simpler, more efficient alternative to React Context for state management.
  • Using context can cause unnecessary re-renders and performance issues in large apps.
  • Zustand stores live outside React, allowing flexible and scalable state management.
  • State updates in Zustand automatically merge, reducing boilerplate code.
  • TypeScript integration with Zustand improves type safety and developer experience.

Summary

  • Introduction to Zustand as a powerful state management library for React applications.
  • Comparison of React Context API and Zustand, highlighting performance issues with context re-renders.
  • Step-by-step guide to converting a basic counter app from context to Zustand.
  • Explanation of Zustand's create function and how to define a store with state and actions.
  • Demonstration of installing Zustand and creating a custom hook for state management.
  • Details on how Zustand merges state updates automatically, simplifying state handling.
  • Use of TypeScript with Zustand for typing state and actions.
  • Benefits of Zustand including better performance and scalability over context.
  • Practical example of implementing increment, decrement, and reset functions in Zustand.
  • Encouragement to replace context with Zustand for real-world React projects.

Full Transcript — Download SRT & Markdown

00:00
Speaker A
Zustand is an incredibly powerful state management library that I've used to build applications.
00:04
Speaker A
From simple counters all the way to completely in-browser card games, all running entirely with Zustand as the state management library.
00:13
Speaker A
In this video, I'm going to show you everything you need to know to get started building your first Zustand project, all the way to what it takes to build out full-scale real-world applications.
00:24
Speaker A
Welcome back to Web Dev Simplified, my name is Kyle, and my job is to simplify the web for you, and to get started, we have a really basic counter application.
00:36
Speaker A
I kind of want to walk through the code though, because there's some important parts about it that you need to understand for this application, we are using context for everything, so we are wrapping our entire application in this counter provider.
00:55
Speaker A
If you're not familiar with context, I'll link a full video in the cards and description telling you everything you need to know about context, but you don't really need to understand what it does to understand this video.
01:02
Speaker A
Essentially, we're just putting all of our code for incrementing, decrementing, resetting, and keeping track of what our count is inside this context.
01:10
Speaker A
This context is then wrapping our entire application, and then we're using a custom hook we called the created use counter.
01:16
Speaker A
And all this does is access the information in our context and give us to it.
01:20
Speaker A
So you can see here, we have our count inside this count display, that's this top card up here, and then we have our counter controls, that's this bottom card down here.
01:30
Speaker A
And here we're getting that increment, decrement, and reset function, and we'll use them to interact with our counter.
01:36
Speaker A
Now the key is, I actually have it so that every single time one of our components re-renders, it'll flash on the screen a separate color, and this render number will increment by one.
01:45
Speaker A
So you can see when I click this plus icon, both of these flash green, and that render number increments by one.
01:50
Speaker A
And you can see, as I click it, every single time that render number is increasing and these are flashing green.
01:53
Speaker A
Now, the reason that this is happening is because when you use context inside of React, every single time you access your context through that hook of use context or whatever custom hook you create.
02:04
Speaker A
It'll re-render every single component accessing that data, so in our case, when our count changes, our display obviously updates.
02:13
Speaker A
But all of our controls down here, which don't have anything to do with our display at all, are also re-rendering.
02:20
Speaker A
This can lead to massive performance problems because if you have a context that's wrapped in most of your application, and you use it in lots of different places.
02:30
Speaker A
It could cause your entire application to re-render every time even small minute pieces of data change.
02:36
Speaker A
Also, context in general just becomes hard to scale as your application grows in size.
02:41
Speaker A
And this is where Zustand really comes in.
02:43
Speaker A
So what I'm going to do is I'm going to show you how to take this simple application, convert it over to a basic Zustand application.
02:50
Speaker A
So we can understand how easy it is to do the comparison between the two.
02:54
Speaker A
So to get started, usually you're going to have like a folder called store or something like that.
03:00
Speaker A
I'm just going to do it in this state folder we already have.
03:03
Speaker A
And I'm just going to create a brand new file called use counter store dot TS, just like that.
03:11
Speaker A
And this is because with Zustand, you're essentially creating a custom hook.
03:16
Speaker A
That's why we prefix this with use.
03:18
Speaker A
Now we need to install the Zustand library.
03:22
Speaker A
So let's just make sure we say NPM I Zustand, just like that.
03:28
Speaker A
That's going to install that Zustand library for you and give you access to everything that you need inside of it.
03:32
Speaker A
Once that's done, we actually have access to a function, which is called create.
03:36
Speaker A
And that allows us to create our store.
03:38
Speaker A
So we can just call create just like that.
03:40
Speaker A
And this create function is a little bit complicated and interesting to work with.
03:46
Speaker A
Essentially, it takes in a single parameter, which is set.
03:50
Speaker A
And this is part of another function.
03:52
Speaker A
So it takes in a function as that parameter.
03:55
Speaker A
And then what we need to do is we need to return an object.
03:58
Speaker A
Of all the different values we want inside of our store.
04:00
Speaker A
So, for example, we may have a count variable that we want.
04:03
Speaker A
Which we're going to set to zero by default.
04:06
Speaker A
And right now, we've essentially created a store.
04:10
Speaker A
We're not even using set.
04:12
Speaker A
We're not updating our state.
04:14
Speaker A
And this store has one single value, which is our count.
04:18
Speaker A
And if we want, we can set that to a variable.
04:21
Speaker A
So we'll just say export const use counter store, just like that.
04:26
Speaker A
And now we've created a custom hook called this use counter store.
04:30
Speaker A
That just stores a count variable in it and nothing else at all.
04:34
Speaker A
Now if we go back into our counter context.
04:38
Speaker A
And actually, let's go into our counter component here.
04:40
Speaker A
We can actually get that information if we want.
04:42
Speaker A
So here, I could say use counter store.
04:45
Speaker A
Call that function just like that.
04:48
Speaker A
And now I get access to all the data that's inside of here.
04:52
Speaker A
So if I want to, I can de-structure out our count variable and have access to that from our counter store.
04:57
Speaker A
Instead of our default counter that we had up here.
05:00
Speaker A
Now you are noticing that I'm getting some errors and stuff.
05:04
Speaker A
And that's because this all requires TypeScript if you want to use TypeScript.
05:08
Speaker A
So in our case, we're going to take this counter state, which I have right here, copy it over into our basic store.
05:15
Speaker A
And we're going to set that as the type.
05:18
Speaker A
And to do that, we just come in here with this create function, it's a generic.
05:23
Speaker A
And we can just paste in the type just like that.
05:25
Speaker A
And now we need to supply those other parameters of increment, decrement, and reset.
05:28
Speaker A
Now you notice, this is very similar to creating a context.
05:32
Speaker A
You can see when we create a context, we pass it a generic, usually we have to default the value to null.
05:40
Speaker A
But the nice thing about Zustand, since it lives entirely outside of React, and you can use it however you want.
05:48
Speaker A
You can just define what your default value is right here.
05:52
Speaker A
You don't have to worry about setting the default to null.
05:55
Speaker A
Then we essentially set all of our different functions, which I'm just going to copy all these functions over.
06:00
Speaker A
So we can use them directly as is.
06:02
Speaker A
And that's pretty much it.
06:04
Speaker A
So essentially, Zustand just replaces context almost one to one.
06:07
Speaker A
When you actually want to use it.
06:09
Speaker A
So we're going to have those functions.
06:12
Speaker A
I want to make them in-line functions.
06:14
Speaker A
Just like this.
06:18
Speaker A
So let me just get those in-lined.
06:21
Speaker A
Make sure I put commas after them.
06:23
Speaker A
There we go.
06:24
Speaker A
And then instead of calling set count, we're going to call this set function.
06:27
Speaker A
And this set function takes in our current state, and then it's going to ask us to return a new value of state.
06:33
Speaker A
Just like when you call set state inside of React, it's the exact same way.
06:38
Speaker A
So we have our current state, which is this entire object right here.
06:43
Speaker A
And we need to return what our new state is.
06:46
Speaker A
Normally, if you're doing this inside of React, you would need to spread out your current state.
06:52
Speaker A
So you'd spread out your current state just like that.
06:54
Speaker A
Make sure I wrap this in parentheses.
06:56
Speaker A
So I know it's being returned.
06:57
Speaker A
There we go.
06:58
Speaker A
And then we could update our count to be our state dot count plus one.
07:03
Speaker A
Just like that.
07:07
Speaker A
But the nice thing about Zustand is essentially anytime that you're updating your state.
07:13
Speaker A
It's going to merge your current state with the new state.
07:17
Speaker A
So this whole adding of the state on like this is not actually required.
07:22
Speaker A
It automatically does that merging for you.
07:25
Speaker A
I essentially now need to do the exact same thing with decrement and reset.
07:28
Speaker A
So here we have our state and we're subtracting one, and here we're just setting our state directly to zero.
07:33
Speaker A
So we can just change this to set zero.
07:35
Speaker A
Again, just like we would work with React.
07:37
Speaker A
And actually, I need to make sure this is count of zero.
07:39
Speaker A
There we go.
07:40
Speaker A
So just like with set state, you can use the function or non-function version.
07:43
Speaker A
And essentially, this set function works just like set state, but instead of updating individual variables, we can use it to update our entire state variable at once.
07:51
Speaker A
We just have one variable in here, which is all we're currently storing.
07:56
Speaker A
So now I have this counter store.
08:00
Speaker A
I have all of my default data stored inside of here.
08:02
Speaker A
I have my type set up for everything.
08:04
Speaker A
Now I can essentially go into my counter here, and anytime I was using that use counter, I can replace it with my new counter store version.
08:11
Speaker A
I can get rid of all of my context stuff.
08:14
Speaker A
I no longer have any context in my application at all.
08:17
Speaker A
And as long as I wrap this in a fragment, everything else should work exactly the same as before.
08:22
Speaker A
But now you'll notice we automatically get some nice performance wins in our application.
08:26
Speaker A
So now you can see here, when I click the plus button, it's actually still rendering both of them.
08:30
Speaker A
As you can see, they're flashing and increasing the render number.
08:32
Speaker A
The reason for this is you don't actually get the performance wins of Zustand.
08:36
Speaker A
Unless you make sure you only extract out the variables you need when you call the use counter store function.
08:42
Speaker A
So if you call this function as is, it returns your entire context object or your entire store.
08:47
Speaker A
So it's returning to us count, increment, decrement, and reset every single time.
08:52
Speaker A
Now in our case, we're only accessing the count variable here, and we're only accessing the functions here.
08:58
Speaker A
So I want to be able to essentially narrow down, only get the data I want.
09:04
Speaker A
That way it'll only update when that data changes.
09:07
Speaker A
You can do that by passing a function to use counter store.
09:10
Speaker A
It'll take your entire state as the value you get for the parameter of the function.
09:15
Speaker A
And then we just want to extract whatever we want.
09:18
Speaker A
So in our case, we want the count variable.
09:22
Speaker A
So now what essentially is going to happen is it's going to extract that count variable out.
09:28
Speaker A
And it's only going to re-render this component when this exact state variable changes.
09:34
Speaker A
I can do the exact same thing down here too.
09:36
Speaker A
So I can say increment is equal to use counter store.
09:40
Speaker A
Or counter store, sorry.
09:43
Speaker A
Take in our state.
09:45
Speaker A
And I want to get our state dot increment function.
09:48
Speaker A
Just like that.
09:50
Speaker A
Now I'm going to do this for our other functions as well.
09:53
Speaker A
Which is decrement and reset.
09:56
Speaker A
Just like that.
09:58
Speaker A
And then I extract them out using the rest operator or the de-structuring just like this.
10:04
Speaker A
This code looks like it works exactly the same.
10:07
Speaker A
As you can see, I get my increment, decrement, reset function and so on.
10:10
Speaker A
But you'll notice nothing's actually rendering.
10:12
Speaker A
And that's because we're technically getting an infinite loop.
10:14
Speaker A
And that's because the way that this works is it does a deep equality.
10:20
Speaker A
So it checks, is this object exactly the same as the object I got last time?
10:26
Speaker A
And since a new object is being created every time this function runs, it's saying this is different.
10:33
Speaker A
So let me re-render the component.
10:35
Speaker A
Again, then the object is different.
10:37
Speaker A
Re-render and so on an infinite number of times.
10:40
Speaker A
If you want to be able to get essentially derived data or create like an object of data to return all at once.
10:48
Speaker A
You can do this by wrapping your entire function call in a call to the hook called use shallow.
10:55
Speaker A
So let's just get that use shallow hook.
10:57
Speaker A
Make sure that this gets imported from Zustand.
11:00
Speaker A
And then we can wrap our entire function call directly in this hook.
11:05
Speaker A
And now everything works exactly the same as before.
11:07
Speaker A
You can see it's only re-rendering the bottom or the top section.
11:12
Speaker A
The bottom section stays the same.
11:14
Speaker A
And the reason that this works is because now it's doing a shallow equality where it checks each key of every single property.
11:20
Speaker A
Instead of doing a deep equality where it's only checking to see if the object reference is exactly the same.
11:25
Speaker A
Now another huge benefit to using Zustand is that you can actually access your entire state independent of React or any framework that you're using.
11:32
Speaker A
All you need to do is just access this use counter store variable anytime you want to access your store.
11:37
Speaker A
So in our application, we have this function called increment outside React.
11:41
Speaker A
You can see when I click plus one, it's still incrementing my count.
11:44
Speaker A
But I'm doing this somewhere outside of my React component.
11:47
Speaker A
Where I don't have access to my increment function.
11:50
Speaker A
But the nice thing is, I don't need to pass this in anymore.
11:53
Speaker A
And that's because I can access my store anywhere I want to.
11:56
Speaker A
So, for example, what I can do inside this section for increment outside React.
12:00
Speaker A
Is I can make sure it takes in no parameters at all.
12:04
Speaker A
And I can access my store, which we called use counter store.
12:07
Speaker A
And here I can call get state to get whatever my current state is.
12:12
Speaker A
Or I can call set state to set whatever my count state is going to be.
12:16
Speaker A
And I can set it to whatever I want.
12:18
Speaker A
For example, here I could set my state to a count of 10.
12:23
Speaker A
And now when I click this button, it's just automatically going to change my count to 10.
12:26
Speaker A
You can see here.
12:28
Speaker A
Doesn't matter what my count is.
12:31
Speaker A
I click this button, it automatically sets it to 10.
12:34
Speaker A
Or I could get my state and I could call that increment function.
12:39
Speaker A
Just like this.
12:41
Speaker A
And now it's essentially going to work just like the normal plus one button.
12:45
Speaker A
They both are going to increment my count by one.
12:47
Speaker A
And this is entirely separate from React.
12:51
Speaker A
Technically, yes, I'm calling this from a click event listener here, but it's accessing this data without being passed anything from React, which is really nice.
13:00
Speaker A
Because now you can access your state variable not only in any component or function you want, but you can even access it outside of your normal React components.
13:08
Speaker A
Which is very, very powerful for managing more complex state in larger scale applications.
13:12
Speaker A
Now another thing you can do with Zustand for some best practices is how you actually store your actions.
13:19
Speaker A
They recommend in the documentation to store your actions inside of your store directly alongside the data that you're storing for those actions.
13:27
Speaker A
This is something that you can do if you want to do.
13:30
Speaker A
You can see that's how we're doing it here.
13:32
Speaker A
And if you do decide to go that route, I highly recommend essentially taking your actions and putting them in a sub-object like this.
13:40
Speaker A
So we can take this actions object and put all of our actions inside of here.
13:44
Speaker A
This just makes it a little bit easier for us to actually work with our code.
13:49
Speaker A
Because now we essentially have this sub-module.
13:53
Speaker A
So it's easier to see, okay, here's my state.
13:56
Speaker A
And here's all the actions that I can perform on that state.
13:59
Speaker A
And you can even break it out into like counter actions.
14:03
Speaker A
Just like that.
14:05
Speaker A
You can essentially take all of these.
14:08
Speaker A
Move those into there.
14:10
Speaker A
And then we're just going to merge these two together.
14:13
Speaker A
Just like that.
14:15
Speaker A
So essentially now I have my actions right here, and here I have my state, which contains my state.
14:20
Speaker A
As well as my actions on top of it.
14:22
Speaker A
And to do that, actually, I need to put actions with my counter actions.
14:25
Speaker A
There we go.
14:27
Speaker A
So that's combining those two different things together if you want to go that route.
14:30
Speaker A
That's one way that you can do it, and inside of our code, all we would need to do to change that to make it work.
14:35
Speaker A
Is first here, call actions dot increment.
14:37
Speaker A
And up here when we're trying to get all of our different information, again, just make sure that we're getting that actions object.
14:42
Speaker A
So we can come in here and we can get state dot actions.
14:45
Speaker A
Just like that.
14:47
Speaker A
Give that a quick save.
14:49
Speaker A
And now everything works.
14:50
Speaker A
And we don't even need to wrap it in a use shallow.
14:53
Speaker A
Because this object never changes inside this section.
14:57
Speaker A
It's always the same object.
14:59
Speaker A
So we don't have to worry about that use shallow context.
15:02
Speaker A
Now, I personally like to just keep my actions outside of my store.
15:07
Speaker A
And my store is essentially a location just for data.
15:11
Speaker A
So what you can do instead is you can essentially create your actions like this.
15:15
Speaker A
So I can create an increment action.
15:18
Speaker A
That's going to set my store.
15:21
Speaker A
So I can say use counter store dot set state.
15:24
Speaker A
Take in my state.
15:27
Speaker A
And I can essentially set the value here for my count to be my state dot count plus one.
15:32
Speaker A
Just like that.
15:34
Speaker A
So now I have an increment function.
15:36
Speaker A
I can remove that.
15:38
Speaker A
And I can paste this down for decrement and reset.
15:41
Speaker A
So here we have reset.
15:42
Speaker A
Here we have decrement.
15:43
Speaker A
This is going to be a minus one.
15:45
Speaker A
And for reset, we're just going to set this to zero.
15:48
Speaker A
Just like that.
15:50
Speaker A
Now again, all these actions I can completely remove.
15:52
Speaker A
I can remove all the actions here and here.
15:54
Speaker A
And now just the actual state information is what's being stored inside of here.
15:59
Speaker A
I don't even need to use this set function at all.
16:01
Speaker A
And then anytime I want to increment, decrement, or reset, I could just make sure that I export these.
16:07
Speaker A
And now I can use them wherever I want.
16:09
Speaker A
So we can come into our code.
16:11
Speaker A
And where we're originally getting this data from our actual store, we can now just directly call the increment, decrement, and reset functions.
16:17
Speaker A
Just like this.
16:19
Speaker A
Same thing here.
16:20
Speaker A
I can just say increment.
16:22
Speaker A
There we go.
16:24
Speaker A
And now you can see, for the most part, everything's working the same.
16:27
Speaker A
But reset is broken.
16:29
Speaker A
That's because here, I need to set my count to zero.
16:31
Speaker A
There we go.
16:32
Speaker A
That should actually solve all the problems I have.
16:33
Speaker A
So now you can see, I can increment, I can decrement, I can reset, plus one from outside React.
16:38
Speaker A
All of that is working exactly the same as before.
16:40
Speaker A
And the nice thing is, is now our actions are entirely separate from our state.
16:45
Speaker A
And only our actual state stuff is what we get from our store.
16:48
Speaker A
Now that's essentially the basics of how you use Zustand, and for 99% of your code, that's all you really need to know.
16:53
Speaker A
But there's a few additional things you can do.
16:55
Speaker A
For example, you can actually make it so that your entire store is persisted inside of some location.
17:00
Speaker A
By default, you can persist to local storage, but you can change that location with custom stuff like the URL or other places.
17:05
Speaker A
In our case, we just want to persist to local storage.
17:08
Speaker A
So what we can do is we can call the persist function from Zustand.
17:13
Speaker A
It's a middleware function.
17:15
Speaker A
And all you do is you just pass it your entire normal function that you have right here.
17:19
Speaker A
So you just wrap all your normal code inside this persist.
17:23
Speaker A
And then as a second parameter to this persist function, we're going to be passing it an object.
17:27
Speaker A
You see, there's a bunch of different stuff I can specify on here, like where I want to store things and so on.
17:34
Speaker A
But for us, I just want to set a name, and that's going to be the name of where this is stored in local storage.
17:38
Speaker A
And let's just say that I want to set this to count.
17:40
Speaker A
Just like that.
17:42
Speaker A
Now you will notice we are still getting some errors.
17:44
Speaker A
Essentially, when you want to use middleware and you're working with TypeScript, what we want to do is we want to make sure we call the create function first.
17:50
Speaker A
And then we call essentially the return of that create function with all of our code.
17:56
Speaker A
So the only change is, you take your normal code.
18:00
Speaker A
And you just call the create function one additional time right before you do all your persist stuff.
18:04
Speaker A
Now this data should be stored inside of local storage under a variable called count.
18:09
Speaker A
So let's just modify this, we're going to change the variable to eight.
18:13
Speaker A
And I refresh my page, and you'll notice it reloads that data of eight.
18:17
Speaker A
It persists properly, and everything works without me having to write any complex code to handle this for me.
18:20
Speaker A
Another optional thing that I pretty much recommend for every use case of Zustand.
18:25
Speaker A
Is that you use Immer or some other type of immutable state management library.
18:29
Speaker A
To make managing nested state much easier.
18:31
Speaker A
Let's take for example that we have some additional state besides our count.
18:34
Speaker A
Let's say that we have a user object.
18:37
Speaker A
This user object can have a name property on it.
18:40
Speaker A
We also inside this user object want to have an address.
18:45
Speaker A
This address may have a street on it.
18:48
Speaker A
And let's just say the street is Main Street.
18:52
Speaker A
And let's say it's also going to have like a zip code or something like that.
18:56
Speaker A
We'll just come in here with a zip code.
18:59
Speaker A
There we go.
19:01
Speaker A
So now we have this brand new user object.
19:03
Speaker A
Let's make sure that we type it out.
19:06
Speaker A
With a string.
19:08
Speaker A
Address.
19:11
Speaker A
That's going to be a string.
19:13
Speaker A
Street is a string.
19:16
Speaker A
And a zip code, which is a string.
19:18
Speaker A
There we go.
19:20
Speaker A
That gets rid of all of our type errors.
19:22
Speaker A
But updating this user object is kind of a pain.
19:25
Speaker A
Let's say that we have a function that's going to be called update street.
19:29
Speaker A
Just like that.
19:31
Speaker A
And we want to update the street to a brand new value.
19:34
Speaker A
Well, what we need to do is first get our counter store.
19:36
Speaker A
So we'll say counter store.
19:38
Speaker A
And we want to set the state.
19:40
Speaker A
We're going to be getting our current state.
19:42
Speaker A
And we want to return a brand new state variable.
19:44
Speaker A
So we're going to be updating our user object.
19:47
Speaker A
And we specifically inside of there want to update the address property.
19:50
Speaker A
And we specifically inside of there want to update the street property.
19:53
Speaker A
To our new street.
19:55
Speaker A
Just like that.
19:57
Speaker A
But you notice we're getting an errors.
20:00
Speaker A
And that's because when I'm writing my state, you notice that it merges like our count here.
20:07
Speaker A
We don't have to define our user or pass in our state.
20:10
Speaker A
Because it merges that top level for us.
20:12
Speaker A
But anytime you have nested state, you need to merge it manually yourself.
20:16
Speaker A
So I need to take all my normal user code and merge that in here.
20:20
Speaker A
And here I need to take all of my address code.
20:24
Speaker A
And I need to merge that in as well.
20:26
Speaker A
When I do that, I now no longer get any errors.
20:29
Speaker A
But you can see managing this is incredibly difficult and painful to do, especially as you get more and more nested code.
20:35
Speaker A
And when you have large global state, it's very common to have deeply nested state objects.
20:39
Speaker A
This is a huge pain to do.
20:41
Speaker A
This is where the idea of Immer or similar libraries comes in.
20:44
Speaker A
All we need to do is just like we used our persist library, we can wrap our code inside Immer instead.
20:49
Speaker A
We don't have to pass along anything additional.
20:52
Speaker A
And we just need to make sure the extra function call is there.
20:54
Speaker A
And when we do that, we can now use Immer to actually make our state updates much easier to work with.
20:59
Speaker A
So in our particular case, let's go down to where we have update street.
21:03
Speaker A
And before we actually do anything, we first need to install Immer.
21:06
Speaker A
So I can just type in NPM install Immer.
21:09
Speaker A
That's going to install the Immer library to use with us.
21:12
Speaker A
And all we need to do is instead of manually typing out what our nested state's going to be.
21:19
Speaker A
The really nice thing is, is now I can just mutate my state directly.
21:24
Speaker A
And Immer will do all the work behind the scenes to stitch everything together.
21:29
Speaker A
So I can say S dot user dot address dot street is equal to new street.
21:33
Speaker A
And that's going to automatically update my address for the street property only.
21:39
Speaker A
And leave everything else exactly the same.
21:41
Speaker A
Now another really nice thing is we could do the same thing with our count variables down here.
21:45
Speaker A
For example, I could just mutate my count by saying count plus plus.
21:49
Speaker A
Which is S dot count.
21:51
Speaker A
That'll increment my count.
21:53
Speaker A
I can do the exact same thing here.
21:55
Speaker A
Where I want to decrement my count.
21:57
Speaker A
And then, of course, down here we're already setting it to zero.
22:00
Speaker A
And now you can see when I click plus, it's updated my count.
22:04
Speaker A
Minus is decrementing my count.
22:06
Speaker A
And reset is setting me back to zero.
22:08
Speaker A
So you can see that Immer just makes it much easier to directly modify my state variables, especially when dealing with nested state.
22:15
Speaker A
Because this one single line is much easier to read and understand.
22:19
Speaker A
And that super long nested bit of code that we had before.
22:23
Speaker A
Now Zustand and state management in general is amazing.
22:27
Speaker A
But sometimes you just don't need it.
22:29
Speaker A
In this video right over here, I talk all about why you may not need a state management library and how you can make do with other features instead.
22:36
Speaker A
With that said, thank you very much for watching and have a good day.
Topics:ZustandReact state managementReact Context APIperformance optimizationTypeScriptcustom hooksReact tutorialstate management libraryWeb Dev SimplifiedReact

Frequently Asked Questions

What is Zustand and why should I use it over React Context?

Zustand is a state management library that offers a simpler and more efficient alternative to React Context. It avoids unnecessary re-renders caused by context updates and scales better for larger applications.

How do I create a Zustand store for my React app?

You create a Zustand store by using the create function, which takes a function returning an object with state variables and actions. This store is accessed via a custom hook that you can use inside your components.

Does Zustand support TypeScript?

Yes, Zustand supports TypeScript. You can define types for your state and actions, and pass them as generics to the create function, improving type safety and developer experience.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →