Redux - Complete Tutorial (with Redux Toolkit)

Cosden Solutions
24 Oct 202337:00

Summary

TLDRIn this comprehensive tutorial, the presenter guides viewers through the essentials of Redux and Redux Toolkit, focusing on the core concepts of the state management library. Starting with an overview of Redux's role in managing global state for React applications, the tutorial delves into the three main components: the store, actions, and reducers. The presenter then demonstrates how to build a simple application using Redux Toolkit, showcasing the ease of creating and connecting state slices, actions, and reducers. The video also covers advanced topics, including asynchronous actions with 'createAsyncThunk' and the use of Redux DevTools for debugging. By the end, viewers are equipped with a solid understanding of Redux and its practical implementation in modern web development.

Takeaways

  • 📚 Redux is a state management library for maintaining global state in React applications.
  • 🏗️ The three main concepts of Redux are the store, actions, and reducers.
  • 🛠️ The store holds the global state and is accessible from any component in the React app.
  • 🔄 Actions describe changes to the state and have a type and an optional payload.
  • 🎯 Reducers are pure functions that determine how the state should be updated based on actions.
  • 📂 Redux Toolkit simplifies the setup and usage of Redux by providing utilities like `createSlice` and `createAsyncThunk`.
  • 🔄 Immutability is a core principle in Redux, meaning the state should never be directly mutated but copied and replaced with updates.
  • 🔗️ React Redux's `Provider` component is used to connect the Redux store to the React app, making the state accessible to all components.
  • 🔑 The `useSelector` and `useDispatch` hooks allow React components to access the state and dispatch actions, respectively.
  • 🛠️ Asynchronous actions can be handled with `createAsyncThunk`, which enables fetching data or performing other async tasks within Redux.
  • 🐛 The Redux DevTools extension is a valuable tool for debugging and visualizing the state changes and actions in a Redux application.

Q & A

  • What is Redux and what does it do?

    -Redux is a state management library that allows you to manage global state in the context of React applications. It enables state to be accessible from any component, regardless of its position in the component tree, making it easier to handle and update application state across different parts of the app.

  • What are the three main concepts of Redux?

    -The three main concepts of Redux are the store, actions, and reducers. The store holds the global state, actions describe changes to the state, and reducers are functions that perform the state changes based on the action type and payload.

  • How does Redux ensure immutability of state?

    -Redux ensures immutability by not allowing direct mutations to the state. Instead, reducers create a copy of the state, make changes to that copy, and then replace the entire state with the updated copy. This prevents unintended side effects and maintains the integrity of the state across the application.

  • What is a Redux toolkit and how does it simplify Redux setup?

    -Redux toolkit is a collection of utilities and conventions that simplify the setup and usage of Redux. It provides functions like 'configureStore' for creating the store, 'createSlice' for generating reducers and actions, and 'createAsyncThunk' for handling asynchronous actions. These utilities reduce boilerplate code and streamline the process of building Redux applications.

  • How do you create a Redux store using Redux toolkit?

    -To create a Redux store with Redux toolkit, you import 'configureStore' from '@reduxjs/toolkit'. You then use 'configureStore' to create your store, passing in an object with a 'reducer' property. Initially, this can be an empty object, and later you can add your reducers from different slices of state.

  • What is the purpose of the 'Provider' component in React Redux?

    -The 'Provider' component in React Redux is used to make the Redux store available to all components in the application. It is a higher-order component that wraps your application, allowing components to access the store and use hooks like 'useSelector' and 'useDispatch' to interact with the Redux state and dispatch actions.

  • How do you create a slice of state in Redux using Redux toolkit?

    -To create a slice of state in Redux with Redux toolkit, you use the 'createSlice' function. This function takes an object with properties like 'name', 'initialState', and 'reducers'. The 'name' is a string that identifies the slice, 'initialState' is the starting state, and 'reducers' is an object where each key is a reducer function that handles specific actions.

  • What is an action in Redux and how is it structured?

    -An action in Redux is an object that describes a change to the state. It has a 'type' property, which is a string that identifies the action, and may have a 'payload' property, which carries additional data needed for the action. Actions are used to trigger updates to the state by being dispatched to the store.

  • How do you handle asynchronous actions in Redux with Redux toolkit?

    -Asynchronous actions in Redux with Redux toolkit are handled using 'createAsyncThunk'. This function creates an action creator that returns a function, which can be an asynchronous operation like an API call. The async thunk action returns a promise, and you can handle the pending, fulfilled, and rejected states in the reducers to update the state accordingly.

  • What is the Redux DevTools and how does it help with debugging?

    -Redux DevTools is a browser extension that provides a visual interface for monitoring and debugging Redux applications. It allows you to track the state changes, inspect actions, and even jump back and forth in time to see the state at different points. This tool is invaluable for understanding the flow of state updates and diagnosing issues in the application.

  • How does the order of defining synchronous and asynchronous actions differ in Redux toolkit?

    -For synchronous actions in Redux toolkit, you first define the reducer and then create the action using 'createSlice'. However, for asynchronous actions, you define the action creator first using 'createAsyncThunk', and then you handle it in the reducers using 'extraReducers' builder, which allows you to handle the pending, fulfilled, and rejected states.

Outlines

00:00

📚 Introduction to Redux and Redux Toolkit

The video begins with an introduction to Redux, emphasizing its role as a state management library for React applications. It explains the concept of a global state that is accessible from any component within the application. The three main concepts of Redux are introduced: the store, actions, and reducers. The video promises to build a simple application using Redux Toolkit to demonstrate how these concepts work together and the ease of using modern Redux.

05:00

🛠️ Setting Up the Redux Store and React Integration

This section delves into the setup process of the Redux store using Redux Toolkit. It covers the creation of a store, the importance of the store's structure, and the initial setup of the Redux provider in React. The video also discusses the need for TypeScript types to facilitate easier state access and the use of the 'configureStore' function from Redux Toolkit to establish the store.

10:02

🔄 Creating the Counter Slice and Initial State

The video demonstrates how to create a counter slice, which is a specific part of the state responsible for the counter functionality. It explains the definition of the state interface, the creation of the initial state, and the use of 'createSlice' from Redux Toolkit to generate the slice and its associated reducer. The process of exporting the reducer and connecting the slice to the store is also covered.

15:03

🎯 Implementing Actions and Reducers for Counter Slice

This part of the video focuses on building actions and reducers for the counter slice. It explains the creation of increment and decrement actions and their corresponding reducers. The video highlights the use of 'createSlice' from Redux Toolkit, which simplifies the process and automatically generates action creators. The importance of immutability in Redux and the correct way to update the state are also discussed.

20:04

🔄 Handling Asynchronous Actions with Redux Toolkit

The video introduces asynchronous actions using 'createAsyncThunk' from Redux Toolkit. It demonstrates how to create an asynchronous action for incrementing the counter by a specified amount and how to handle this action in the reducer. The concept of 'pending', 'fulfilled', and 'rejected' states in asynchronous actions is explained, along with the use of 'extraReducers' to handle these states.

25:06

🌐 Debugging Redux Applications with Redux DevTools

The final section of the video showcases the use of Redux DevTools for debugging Redux applications. It explains how to install and use the tool to monitor actions, track state changes, and inspect the application's state at different points in time. The video emphasizes the benefits of Redux DevTools in understanding the flow of actions and their impact on the state, making it easier to debug and develop Redux applications.

Mindmap

Keywords

💡Redux

Redux is a state management library used primarily with React applications. It allows developers to manage and maintain the state of an application in a predictable and centralized way, which is crucial for building large and complex applications. In the video, Redux is used to handle the global state of the application, enabling state access from any component within the React app.

💡Store

In the context of Redux, the store is the central object that holds the entire state tree of the application. It is the single source of truth for the application state, and changes to the state can only occur through specific actions dispatched to the store. The video emphasizes the importance of the store and demonstrates how to set it up using Redux Toolkit.

💡Actions

Actions in Redux are plain objects that represent an intention to change the state. They contain a 'type' property that indicates the nature of the action, and may also include a 'payload' with data needed to perform the action. Actions are the only way to trigger state changes in Redux and must be handled by reducers to update the state accordingly.

💡Reducers

Reducers are pure functions in Redux that take the previous state and an action as input, and return the next state. They are responsible for computing the new state based on the current state and the dispatched action. Reducers must be immutable, meaning they do not change the existing state but return a new state object after each action is processed.

💡Immutability

Immutability in the context of Redux refers to the principle that state should not be changed directly. Instead, a new copy of the state should be created with the desired changes, and the old state should be replaced with this new version. This ensures that the state changes are predictable and traceable, which is essential for debugging and maintaining the application state.

💡Redux Toolkit

Redux Toolkit is the standard library for building Redux applications. It provides a simplified and streamlined way to work with Redux, including utilities for creating slices, actions, reducers, and store configurations. The toolkit aims to make Redux easier to use by reducing boilerplate code and providing a more modern approach to state management.

💡Provider

In React applications using Redux, the Provider is a component from the react-redux library that makes the Redux store available to all components in the application via React context. By wrapping the root component of the app with the Provider, all child components can access the Redux store and dispatch actions or subscribe to state changes.

💡Selectors

Selectors are functions that select and return a piece of the state from the Redux store. They are used to compute derived data from the store and can be used to pass state to React components in a controlled and efficient way. Selectors can also be memoized to improve performance by preventing unnecessary recalculations.

💡Async Thunk

Async Thunk is a middleware library for Redux that allows for the creation of actions that can handle asynchronous operations, such as API calls. It enables actions to dispatch other actions and perform complex asynchronous logic before committing to the final state change. This is particularly useful for handling operations that require waiting for a response before updating the application state.

💡Redux DevTools

Redux DevTools is a browser extension that provides a debugging and visualization tool for Redux applications. It allows developers to track the state changes, inspect action logs, and even jump back and forth in time to see the state at different points. This tool is invaluable for understanding the flow of actions and state changes in complex applications.

Highlights

The introduction of Redux as a state management library for global state in React applications.

Explanation of the three main concepts of Redux: store, actions, and reducers.

The store being a global state accessible from any component in the React application.

Actions being used to tell Redux what to do with the state, with two important properties: type and payload.

Reducers are responsible for making updates to the Redux store based on action types, adhering to the concept of immutability.

Creating a simple application using Redux Toolkit to demonstrate how the different pieces come together.

Explanation of how to create a Redux store using Redux Toolkit and connect it to a React application.

Demonstration of creating a counter slice within Redux, including defining state, initial state, and reducers.

How to use the `createSlice` function from Redux Toolkit to simplify the process of setting up reducers and actions.

Building a counter application and connecting it to Redux, showcasing the use of actions and reducers in a practical example.

Introduction to asynchronous actions using `createAsyncThunk` from Redux Toolkit.

Explanation of how to handle different states in asynchronous actions: pending, fulfilled, and error.

The importance of defining action names for asynchronous functions manually, unlike synchronous actions.

Demonstration of using the Redux DevTools for debugging and visualizing the state changes and action dispatches.

How Redux DevTools allows you to jump back and forth in time to see the application's state at different points.

The convenience of Redux Toolkit in automatically handling action names and providing a cleaner setup for Redux slices.

The comprehensive tutorial covering Redux and Redux Toolkit provides a complete understanding, aiming to eliminate the need for further videos on the topic.

Transcripts

play00:00

what's up guys there is cin here welcome

play00:02

to the last Redux with Redux toolkit

play00:05

tutorial video you're ever going to have

play00:06

to watch I promise you if you watch this

play00:09

video and you watch it until the end

play00:11

you're never going to have to watch

play00:12

another video on this ever again all

play00:14

right cool we're now on my computer and

play00:16

we can begin the first thing that I want

play00:18

to do is just spend a couple of minutes

play00:20

not too long just talking about Redux as

play00:22

a whole and also talking about the three

play00:25

main concepts of Redux the store actions

play00:27

and reducers and then we are going to

play00:30

built a simple application using weux

play00:32

toolkit so that you get to see how all

play00:34

of the different Pieces come together

play00:36

and also how easy it is to work with

play00:38

modern Redux using Redux toit all right

play00:41

so let's talk about Redux Redux is

play00:43

essentially a state management library

play00:46

that allows you to have Global state in

play00:48

the context of react what this means is

play00:50

that you can have state that is

play00:51

accessible from any component no matter

play00:54

where they are in the tree this is why

play00:56

it's called a state management library

play00:57

and it makes it really really easy to

play00:59

configure figure and to have Global

play01:01

state in your react applications so

play01:02

Redux has three different concepts and

play01:04

they're all super super important the

play01:06

first concept is the concept of a store

play01:08

so I'm just going to write store here

play01:10

Store the store is essentially state it

play01:13

is the global state that as I've just

play01:15

mentioned is going to be accessible

play01:17

across any component no matter where

play01:19

they are in your react application the

play01:21

store you get to Define it in any way

play01:23

that you want you can put whatever you

play01:24

want in there it's completely up to you

play01:26

and the needs of the application and

play01:28

Redux with Redux tool kit will allow you

play01:30

to easily set this up and then connect

play01:33

it to your react application now the

play01:35

store will usually be made up of

play01:37

multiple slices multiple slices of the

play01:39

state each responsible for a certain

play01:41

domain in the application for example

play01:43

let's say that we had a counter

play01:45

application we would have a counter

play01:47

slice a counter slice of our state which

play01:49

would hold the pieces of state that are

play01:51

specifically related to the counter so

play01:53

for example we could do something like

play01:56

interface and then

play01:58

counter State and we can Define whatever

play02:01

state we want for this counter for

play02:03

example we could do value and that is

play02:05

going to be of type number we would have

play02:08

a slice of our state called the counter

play02:10

state which would hold the value of our

play02:12

count which is of type number and then

play02:14

that would be one part of our redu state

play02:16

then we could maybe have another piece

play02:18

of our state for example

play02:20

interace and then we can do user state

play02:24

with a capital and that will hold all of

play02:27

the pieces of State for our user for

play02:29

example is signed in right is the user

play02:33

currently signed in is the user

play02:35

currently sign out now of course I'm

play02:37

grossly over simplifying this in a real

play02:39

application you would have a lot more

play02:40

than this but the point is that your

play02:42

state your Global store in rux is going

play02:45

to be made up of multiple slices and

play02:47

they all come together to form one

play02:49

Global state that is going to be

play02:50

accessible in your entire application

play02:52

then the next Concept in Redux is the

play02:54

concept of actions so I'm going to come

play02:56

here and do actions actions are

play02:59

essentially what you use to tell weux

play03:01

what it should do to the state in the

play03:03

case of our counter State here we have a

play03:06

value which is of type number which

play03:07

means that we might have an action to

play03:09

increment the count to make this Value

play03:11

Plus one and we also might have an

play03:13

action to decrement the count to make

play03:15

this value minus one actions and Redux

play03:18

have two properties that are super super

play03:19

important so let me just write some code

play03:21

here to illustrate this this is not like

play03:23

real code this is pseudo code just to

play03:25

illustrate you what I mean with this

play03:26

example I'm going to do const increment

play03:30

that is going to be equal to an object

play03:32

which is going to have a type that type

play03:34

is going to be increment I'm just going

play03:36

to remove this here and then we also

play03:38

have payload which is going to be one

play03:41

this is what an action looks like and

play03:42

let me just do the same thing for a

play03:44

decrement so I'm just going to come here

play03:45

and do

play03:47

decrement now we have two pseudo actions

play03:50

remember this is pseudo code this is not

play03:52

real code they have a type which is

play03:54

always going to be of type string and

play03:56

this is required this is essentially the

play03:59

name of this action right reux is going

play04:01

to use this type to determine what it

play04:03

should do to the state if it receives an

play04:06

action with a type of increment it'll

play04:08

know that it has to increment the state

play04:10

by one if it receives an action with a

play04:12

type of decrement it'll know that it has

play04:14

to decrement the state by one then we

play04:17

have this payload here which is the

play04:18

second property of an action this one is

play04:21

not required this is optional the

play04:23

payload is essentially any data that you

play04:25

want to send to Redux in your action so

play04:28

that it can perform its job let's say

play04:30

that instead of increment we had an

play04:32

action that was increment by amount and

play04:34

we needed a certain amount to increment

play04:36

our state by our count buy we would use

play04:38

this payload to pass the actual value

play04:41

that we want to increment our state buy

play04:43

that is going to be the payload of this

play04:45

action but in this case in the case of

play04:47

increment we don't actually need a

play04:50

payload because increment really just

play04:52

means increment by one so in this

play04:54

specific case we could just go without

play04:56

this payload we can just remove it here

play04:57

in both cases because because you don't

play05:00

want to increment by anything else other

play05:02

than one so you don't actually need a

play05:04

payload but if you had an action for

play05:06

example increment by

play05:09

amount right we would then need to have

play05:11

a specific payload cuz we don't know

play05:13

what amount we want to increment the

play05:15

state by so in this case to Redux we

play05:17

would send a payload in this case of 10

play05:19

it can be anything you want this payload

play05:22

doesn't have to be a number it's only a

play05:23

number because we're working in our

play05:25

counter State suro example right but you

play05:27

can literally Define your state and your

play05:29

actions in any way and you can send

play05:31

whatever payload you want then we have

play05:33

the third Concept in rux which is the

play05:36

concept of reducers reducers essentially

play05:38

are responsible for taking an action and

play05:41

then depending on the type of the action

play05:43

will actually go out and make the update

play05:46

in the Redux store they will use the

play05:48

type of the action to know what updates

play05:49

to do and optionally they will use the

play05:52

payload to do those specific actions to

play05:54

make those specific updates to the Redux

play05:56

store now one very important thing to

play05:58

know about reducers and also to know

play06:00

about Redux in general is the fact that

play06:02

reducers will never directly make an

play06:05

update to the Redux store we have this

play06:07

concept of immutability which means that

play06:09

we're never allowed to directly mutate

play06:12

the state instead what reducers are

play06:14

going to do is they're going to take the

play06:16

state they're going to make a copy of

play06:17

the state and then make those changes to

play06:20

that copy of the state which will also

play06:22

have all the other unchange properties

play06:24

of the state and then will completely

play06:26

replace the state as a whole with the

play06:29

copy that has the changes applied we're

play06:31

never going to directly mutate the state

play06:33

Redux does not work if you mutate the

play06:36

state directly you always have to follow

play06:38

the concept of immutability make a copy

play06:41

to the state make the changes to that

play06:43

copy and then override the entire State

play06:46

this is very important because this is

play06:47

how Redux Works cool so now with this we

play06:50

fully understand Redux we understand the

play06:52

store actions and reducers and how they

play06:54

all work together which means that we

play06:56

have everything that we need to actually

play06:58

start building our simple application

play07:00

with Redux toolkit so that you actually

play07:02

get to see how to implement the store

play07:04

the actions and the reducers and how to

play07:06

connect all of it together easily with

play07:08

Redux toolkit so the first thing that we

play07:10

need to do is we need to create our

play07:12

store so I'm going to come here to my

play07:14

file explorer I'm going to make a new

play07:16

folder and I'm going to just keep things

play07:17

very simple I'm going to call this

play07:19

folder state so we'll do state and

play07:22

inside of this folder I'm going to

play07:23

create a new file and this one is going

play07:25

to be store. TS this file is going to

play07:28

hold our rux store so what we need to do

play07:30

to create a store is we need to import

play07:32

something from Redux toolkit so I'm

play07:34

going to do import configure actually

play07:38

let me just strike in

play07:40

import configure store and we have the

play07:43

import right here we're going to import

play07:45

configure store from rxjs toolkit and

play07:47

that is going to allow us to easily

play07:49

create our store then we're going to

play07:51

come here and we're going to do export

play07:54

const store equals configure store this

play07:58

is going to take a PR es and then an

play08:00

object as a parameter and then this

play08:02

object will only take one key and that

play08:04

is going to be reducer and for now

play08:06

because we don't actually have any

play08:08

reducers we're just going to give this

play08:10

an empty object but as soon as we create

play08:12

our different reducers our different

play08:14

slices of our state as we've seen here

play08:16

we're going to then come here and

play08:18

connect them using this store so that

play08:20

then they will be accessible across our

play08:22

entire application and finally what we

play08:24

need to do because we are working in

play08:26

typescript we're going to need to export

play08:27

two types that are going to be very very

play08:29

useful as we work with all of the

play08:31

different components of Redux and Redux

play08:32

toolkit in our react application so

play08:35

already copilot is being very helpful it

play08:37

knows exactly what I want to export the

play08:39

first type that I want to export if I

play08:41

can just do it is going to be the root

play08:43

state so this is essentially the return

play08:46

type which is a typescript helper

play08:47

utility of the type of store.get state

play08:51

so this configure store gives us a store

play08:53

variable here which has a get State

play08:56

function and then we can get the return

play08:58

type of that function to have as our

play09:00

root state so then in any react

play09:02

component as you're going to see

play09:04

whenever we need to access the state

play09:05

using a selector we're going to be able

play09:07

to define the state using this root

play09:09

State type and then we'll have access in

play09:12

typescript to all of our state super

play09:14

easily then right below we're going to

play09:16

export another type and that is going to

play09:18

be the app dispatch type which is also

play09:20

going to be very useful so I'm going to

play09:22

come here and do export type app

play09:25

dispatch and then of course copilot

play09:27

knows exactly what I want this comes

play09:29

from the type of store. dispatch so we

play09:32

also have another function on the store

play09:34

called dispatch and we can get the type

play09:36

of that function and then export this as

play09:38

this app dispatch type you're going to

play09:40

see this is going to come very useful

play09:42

when we're trying to do asynchronous

play09:44

actions using our used dispatch hook

play09:46

cool so now with this we've successfully

play09:48

created our Redux store the next step

play09:50

for us to do is to connect this store to

play09:52

react because react by default cannot

play09:55

directly talk to Redux we have to use a

play09:57

provider from react Redux to then

play10:00

connect our store and our entire Redux

play10:02

application state to react so what I'm

play10:04

going to do I'm going to come here in my

play10:06

main file wherever I'm basically

play10:08

rendering the entire app and then I'm

play10:10

going to import a

play10:13

provider if I can type properly I have a

play10:15

new keyboard that is why this is a

play10:17

little bit slower Provider from react

play10:20

Redux and then also we're going to

play10:22

import our store that we just created so

play10:24

I'm going to come here and do import

play10:26

store and that is going to be from.

play10:30

State SL store this is exactly the store

play10:32

that we just created right now this

play10:34

provider comes directly from react vux

play10:36

and not from toolkit which is what is

play10:38

going to allow us to connect our Redux

play10:40

Store to our react application this

play10:42

provider works with the Redux context

play10:44

API which if you're not familiar with it

play10:46

I do have a whole tutorial video on that

play10:48

that you can watch to get yourself up to

play10:50

speed and essentially we are going to

play10:52

wrap our entire app with this provider

play10:54

provide our store to the provider which

play10:56

is then going to allow us to use the

play10:58

store in any react component so I'm

play10:59

going to come here make a new line and

play11:01

then just do provider I'm going to give

play11:04

it the store here and then write below

play11:07

the app I'm just going to make another

play11:09

line and then just close this provider

play11:11

tag and then save and then actually

play11:13

remove this because this is unneeded and

play11:15

this is what we need to now connect

play11:17

react to Redux so we use this provider

play11:19

we injected it to store using context

play11:22

which means that that now this store is

play11:24

going to be accessible to our entire app

play11:26

and any component no matter where they

play11:28

are in the tree it's really really

play11:30

useful now that we have our provider set

play11:32

up we can actually go and create our

play11:34

first slice of state so what we're going

play11:36

to build in this tutorial video is going

play11:37

to be a simple counter application so

play11:40

we're going to need to create our

play11:41

counter slice so I'm going to come here

play11:43

in my state folder I'm going to make a

play11:45

new folder called this counter this is

play11:47

going to hold our counter slice and then

play11:49

I'm going to make a new file and call it

play11:51

counter slice. TS this file is going to

play11:55

contain everything that we need anything

play11:57

that is related to our counter slice our

play11:59

actions our reducers our state they're

play12:01

all going to go in this file so the

play12:03

first thing that we need to do because

play12:05

we are working in typescript we need to

play12:07

Define our state so what I'm going to do

play12:08

is do

play12:10

interface counter State and then we're

play12:13

going to just give it value and that is

play12:15

going to be of type number we're not

play12:17

going to give it anything else because

play12:18

frankly we don't need anything else in

play12:20

our state then the next step is we need

play12:22

to actually create our initial state I'm

play12:24

going to let copilot here be very

play12:26

helpful we have initial state which is

play12:28

going to be a type counter State and

play12:30

that is going to initialize the value to

play12:32

zero which means that whenever we're

play12:34

initializing the state it's always going

play12:36

to start with the value being zero then

play12:38

what we need to do is come here and

play12:40

create our actual slice so I'm going to

play12:42

do const counter slice and that is going

play12:45

to be equal to create slice and I'm

play12:47

going to import this directly from rxjs

play12:49

toolkit that is going to take a

play12:51

parenthesis and then it's going to take

play12:53

an object as a parameter the keys that

play12:55

we're going to give to this object are

play12:57

the name that is going to be the name of

play12:59

our slice which is going to be counter

play13:01

we don't want to do anything fancy right

play13:02

this is a simple name for a simple slice

play13:05

then it's going to take in an initial

play13:07

state which we can just give it here

play13:08

initial state that is going to be the

play13:10

state that this slice will start with

play13:12

and then we can give it some reducers so

play13:14

I'm just going to do reducers and then

play13:17

for now we're not going to have any

play13:18

actual reducer so this is just going to

play13:20

be a simple empty object and then we're

play13:23

going to come here at the bottom and do

play13:24

something really really cool I'm going

play13:26

to do export default count C

play13:30

slice. reducer and then save this

play13:33

essentially because we're using Create

play13:35

slice from Redux JS toolkit this gives

play13:38

us a lot of benefits a lot of cool

play13:40

things behind the scenes without us

play13:42

having to set up anything just by

play13:44

defining this slice here we

play13:46

automatically get access to a reducer

play13:49

which we're exporting here by doing

play13:50

counter slice. reducer this is only

play13:53

available because we're using crate

play13:55

slice from rxjs toolkit and it's doing a

play13:57

lot of the magic for us if we weren't to

play13:59

use this we would have had to do a lot

play14:01

more boilerplate code to achieve the

play14:03

same functionality so now with this

play14:05

we've efficiently and easily exported

play14:08

the reducer as the default export of

play14:10

this file which means that we can now

play14:12

come here to our store where we defined

play14:13

our store and we had this empty reducer

play14:15

key here and we can import that reducer

play14:18

that we just created so I'm going to

play14:19

come here and do import counter reducer

play14:23

from and that's going to be dot counter

play14:27

SLC counter slice essentially this is

play14:29

the reducer that we just exported here

play14:31

as the default export then inside of

play14:34

this reducer object here instead of it

play14:36

being empty I'm going to do counter

play14:39

counter reducer and now we've

play14:41

essentially connected the counter slice

play14:43

to our store which is then going to be

play14:45

accessible using this provider to our

play14:47

entire react application the beauty of

play14:49

setting it up this way is that you can

play14:51

have as many slices as you want as we've

play14:54

seen before right we had the counter

play14:56

State here we had the user State we can

play14:58

have any number of states we would just

play15:00

create a slice for each of those States

play15:02

they would have their own individual

play15:03

slices of state and very soon we're

play15:05

going to see actions and reducers and

play15:07

then you can just combine all of them

play15:09

here which makes it really really easy

play15:11

to have separation of concerns each

play15:13

slice is responsible for its own State

play15:15

and nothing else and it makes everything

play15:17

really organized and really really easy

play15:19

to work with cool so now let's actually

play15:21

build some actions and reducers now

play15:23

Redux toolkit makes this really really

play15:25

simple the only thing that we need to do

play15:27

to create both AED reducer and an action

play15:30

is just toine a reducer here so I'm

play15:32

going to make a new line here and what

play15:33

I'm going to do is I'm just going to do

play15:35

increment and that is going to be our

play15:38

reducer and reducers they always take a

play15:41

state as a parameter and then they will

play15:43

just return here and let me just see if

play15:45

copilot can do it yes it can so reducers

play15:48

you have to Define them by a name you

play15:49

have to give them a key and then they

play15:51

will always take a state and optionally

play15:54

if I can just type properly my keyboard

play15:56

is really hard to get used to they will

play15:59

also take here an action but this is

play16:01

optional this doesn't always need to

play16:03

happen because it can happen in the case

play16:06

of increment for example where you don't

play16:08

actually need to use anything from the

play16:09

action you just know that you have an

play16:11

increment and you just need to update

play16:13

the state to plus one so in this case

play16:15

specifically for the increment reducer

play16:17

we're just not going to give this

play16:19

because this is going to be an unused

play16:20

property now there's something really

play16:22

interesting that I want you to pay

play16:23

attention to because this is super

play16:25

important remember how in the beginning

play16:27

of the video I said that Redux you never

play16:30

mutate the state directly you always

play16:32

make a copy to the state and then you

play16:34

replace the entire state with it well

play16:36

here it seems that this increment

play16:37

reducer will take the state and then

play16:40

will directly assign state. Value Plus

play16:43

equals 1 isn't this directly mutating

play16:45

the state is this a mistake well no this

play16:48

isn't a mistake but at the same time I'm

play16:50

not sure how I feel about this because

play16:52

this can get really confusing especially

play16:54

to someone that's just starting out with

play16:56

Redux essentially what's happening is

play16:58

because we're using crate slice from

play17:00

Redux toolkit as I've said this does a

play17:03

lot of the magic for us behind the

play17:05

scenes so that we don't have to do it

play17:07

and essentially What's Happening Here is

play17:09

this create slice is smart enough to

play17:11

know that we want to make this update to

play17:12

the state so it allows us to write

play17:15

mutating code right which I put in

play17:16

quotes because we're not actually

play17:18

mutating it and what it is going to do

play17:20

is behind the scenes it's going to make

play17:22

its own copy of the state apply this

play17:24

change to that copy and then return to

play17:27

us the value that new state then

play17:29

overwrite this allows us to write

play17:31

mutating code which makes it easier for

play17:33

us because we don't have to make all

play17:34

those copies and manage those but at the

play17:37

cost of it being a little bit confusing

play17:39

because it does look like we're writing

play17:40

mutating code so just remember because

play17:42

this is really important you can only do

play17:45

this if you're using Create slice from

play17:46

Redux otherwise you cannot do this and

play17:49

you would have had to make your own copy

play17:50

of the state and do all of that manually

play17:52

but because we're using this we don't

play17:54

have to do it and it's handled

play17:56

automatically for us which Again mix

play17:58

feeling LS it's convenient but it's also

play18:00

confusing great so now that we

play18:02

understand that we can make our next

play18:03

reducer which is going to be the decr

play18:05

case this is going to be very simple

play18:07

it's going to be exactly the same thing

play18:08

we're going to do

play18:10

decrement that is also going to take the

play18:12

state here and also that is going to do

play18:14

state. value minus one again because

play18:16

we're using Create slice we can do this

play18:18

and it's not going to be a problem we're

play18:20

not actually going to mutate the state

play18:21

directly so now these are our reducers

play18:24

we've just told reux that we have two

play18:26

reducers increment and decrement and

play18:28

this is how the state should update

play18:29

whenever we have actions that actually

play18:31

want to trigger those reducers so then

play18:33

how do we create those actions well this

play18:35

is another point of the magic of Redux

play18:37

toolkit we can easily get access to the

play18:40

actions by just doing export con and I'm

play18:43

going to let copile it be very helpful

play18:45

we're going to export con increment and

play18:47

decrement from counter slice. actions so

play18:51

just like we were able to get access to

play18:52

the reducer from this counter slice

play18:54

using rux toolkit we're also able to get

play18:57

the action directly from this counter

play18:59

slice and it is smart enough to know

play19:01

that we only have an increment action

play19:03

and a decrement action so this makes it

play19:05

really really easy because we don't have

play19:07

to write any extra code to create our

play19:10

actions and now we've exported our

play19:11

actions which means that we can directly

play19:13

use them in our react components so we

play19:16

can then come here to our app we can get

play19:18

rid of all of this introductionary code

play19:20

that we had before we can save this and

play19:22

then we can go to our counter component

play19:24

which for now is empty so now what we're

play19:26

going to do is we're going to connect

play19:28

this counter component to Redux to

play19:30

access the state and also we're going to

play19:32

connect it so that it's able to dispatch

play19:34

some actions so the first thing that we

play19:36

need to do is we need to connect this to

play19:38

the state so I'm going to make here a

play19:39

new line and I'm going to do const count

play19:42

is going to be equal to use selector

play19:45

that is going to come from react Redux

play19:48

this is a function this is a hook so it

play19:49

takes a parenthesis and then it also

play19:51

takes a state as a parameter and then it

play19:54

will return something here so let's just

play19:56

do state. count this is not correct

play19:58

correct because we haven't yet added any

play19:59

types to our state now remember if I go

play20:02

here to the store we've exported two

play20:04

different types here one of these types

play20:06

is the root state so we're actually

play20:07

going to use this root state to Define

play20:09

our state here so I'm just going to do

play20:11

here and then root State import this

play20:14

from state. store and now it knows that

play20:16

state. count is not valid because that

play20:18

is not how we defined our state so I'm

play20:20

going to come here remove this and then

play20:22

do state DOT and now it knows that we

play20:24

have a counter slice and then I can do

play20:26

dot again and that it knows we have a

play20:28

value which is going to be of type

play20:31

number because that is how we defined it

play20:33

in our counter slice here which means

play20:35

that now this count variable is of type

play20:37

number because everything is correctly

play20:39

typed this is how you connect react to

play20:42

Redux you have to use your selector and

play20:44

then you get the state and then you can

play20:46

select literally anything that you want

play20:48

we could have selected anything besides

play20:50

the count but in this case we want the

play20:51

count so that is how we got the count

play20:53

then the next thing that we need to do

play20:55

is we need to connect this to dis patch

play20:57

so that we're able to to dispatch

play20:58

actions to our Redux store so I'm going

play21:00

to do here const dispatch and that is

play21:03

going to be equal to use dispatch which

play21:05

is also going to come directly from

play21:07

Redux we are going to use this dispatch

play21:10

to dispatch actions because as I've said

play21:12

react cannot directly talk to Redux we

play21:14

need to use some of the hooks from react

play21:16

Redux to allow us to have that

play21:18

functionality to connect between a react

play21:20

component and our Redux store then I'm

play21:22

just going to make some space here

play21:23

create a new line and then we can

play21:25

actually build out our component so the

play21:27

first thing that I want to do is maybe

play21:28

something like H2 and then put our count

play21:32

here my God it's extremely difficult to

play21:35

type with this keyboard I can not for

play21:36

the life me type count there you go

play21:39

finally after a th years and then I'm

play21:41

going to create a new div here with

play21:43

maybe two buttons a button here to let

play21:47

me just fix this a button here to

play21:50

increment and then a button here

play21:54

to to decrement right currently there's

play21:56

no functions attached to these and we're

play21:58

going to change that in just a moment so

play22:00

button on click that is going to be

play22:03

equal to here we're going to create a

play22:05

parenthesis create this and this and now

play22:07

we are going to

play22:09

dispatch and dispatch takes in an action

play22:12

and that is going to be our increment

play22:14

which again we can import directly from

play22:16

our counter slice so this is essentially

play22:18

what we've exported here we've exported

play22:21

this increment action which we can then

play22:23

import in our counter example and then

play22:25

we can dispatch it but because it's a

play22:27

function we also need to put a

play22:29

parenthesis here then we're going to do

play22:31

the same thing here for decrement on

play22:33

click equals here and then open the

play22:36

parentheses and then Arrow function

play22:39

dispatch decrement import this as well

play22:42

and then call this and we are done this

play22:45

is how you dispatch actions in Redux you

play22:48

use this dispatch from use dispatch and

play22:50

then you use the actions that come from

play22:52

the actions here of our slice and then

play22:54

you can easily dispatch them and then

play22:56

everything else is going to be handled

play22:58

automatically for you now if we go back

play23:00

to our application we now see our

play23:02

counter which is initialized with zero

play23:04

and then we have a button to increment

play23:06

and a button to decrement the count if I

play23:08

press it it's going to increment by one

play23:10

if I press decrement it's going to

play23:11

decrement and essentially this works

play23:13

exactly as we expect we have a full

play23:16

working counter example implemented in

play23:18

Redux with Redux tokit great so now that

play23:21

we've seen how this works let's create

play23:23

another action this time with an

play23:25

argument cuz right now our increment and

play23:26

decrement actions didn't not take any

play23:28

argument so we're going to come here go

play23:30

back to our slice and then we're going

play23:32

to create a new reducer and this one

play23:34

this time is going to be increment by m

play23:38

if I can type increment by

play23:42

amount and this one is going to take the

play23:44

state but also this time it's going to

play23:47

take an action because we're going to

play23:48

need to pass a payload to be able to set

play23:51

by how much we want the state to be

play23:52

incremented by so I'm going to make this

play23:55

equal Arrow function and then let's see

play23:57

what Copilot it does yes it's smart

play23:59

enough to figure that it can use the

play24:01

payload of the action to increment the

play24:03

state bu and then because we are working

play24:05

in typescript what I want to do is give

play24:07

this a type so I'm going to come here

play24:09

and then do

play24:11

payload payload action we're going to

play24:14

import this directly from reux toolkit

play24:16

and then we're going to give it a type

play24:18

it's going to be number save this and

play24:21

then we have increment by amount takes

play24:23

in the state as with any other reducer

play24:26

and then takes in an optional action

play24:28

with an optional payload and that

play24:30

payload is going to be of type number

play24:32

which means that we can do state. Value

play24:34

Plus equals action. payload this payload

play24:37

you could Define it in any way that you

play24:39

want instead of number we could have had

play24:40

something like an object here that had

play24:42

value and then we could do number and

play24:44

then this would no longer work because

play24:46

we would have to do actually value to

play24:48

then get the actual number so it's

play24:50

number plus number right but we're not

play24:51

going to do that we're just going to

play24:52

have count because we want to keep the

play24:54

symbol and really all we need is a count

play24:56

but again you can find this in any way

play24:58

that you want that's the whole point of

play25:00

Redux is that it depends on the

play25:02

application and what it needs and you're

play25:03

free to configure this in any way that

play25:05

you want and then to get access to this

play25:08

action all that we have to do is come

play25:10

here and do increment by amount and

play25:12

again it already knows that this is an

play25:14

action so we can easily just use it in

play25:16

our counter component we can come here

play25:18

do increment by amount it already knows

play25:21

and then instead of increment we can do

play25:22

buy amount and then we can pass it here

play25:24

10 for example save this go back to

play25:27

application

play25:28

increment it's going to increment by 10

play25:30

increment Again by 10 Again increment

play25:32

again when I press decrement this one is

play25:34

still doing it by one so this

play25:36

application works exactly as you expect

play25:39

this is how easy it is to create any

play25:40

number of actions you just Define the

play25:42

reducer you export the action and

play25:45

everything just works easily now let me

play25:47

show you something cool and really

play25:48

useful we're going to take a look at an

play25:50

asynchronous action because the reality

play25:53

is if you're working with three dogs

play25:55

often times you're going to have to do a

play25:56

synchronous action for example you want

play25:58

to fetch some data from an API that has

play26:01

to be asynchronous and there's a little

play26:03

bit more differences when you're doing

play26:04

that versus a simple action that is

play26:07

synchronous so let's come here and we'll

play26:09

Define our asynchronous action we're

play26:11

going to do export const increment

play26:16

async that is going to be equal to

play26:18

something new that is going to be equal

play26:20

to

play26:22

create async thunk this is also going to

play26:25

come from meix toolkit this is a paren

play26:27

es and then it takes a bit of arguments

play26:29

so the first argument is going to be the

play26:31

name of the action so we're just going

play26:33

to do counter SL

play26:37

increment

play26:39

async can I type

play26:41

increment

play26:44

async a sync and that is going to be a

play26:47

comma and then it's going to take the

play26:49

actual function and hopefully we can let

play26:51

copilot do the work great this is going

play26:54

to be an asynchronous function it's

play26:56

going to take in our case a a parameter

play26:58

that is going to be amount that is going

play26:59

to be of type number it is going to then

play27:02

wait for 1 second which is going to

play27:04

simulate waiting for something to happen

play27:06

this can simulate for example fetching

play27:08

data from an API which may very well

play27:10

take a second and then it's going to

play27:12

return the amount it's simple it's a

play27:14

simple example while we're mocking an

play27:16

asynchronous function but essentially

play27:19

we're just returning a value here which

play27:20

is going to then be the payload of this

play27:22

action now there's two very important

play27:24

things that we just did here the first

play27:26

one is the name we haven't actually

play27:28

defined any name for any of these other

play27:30

actions here in the reducers that is

play27:32

because reux toolkit will automatically

play27:35

do that for us so essentially every

play27:37

single one of these this is going to be

play27:38

counter SL increment counter SL

play27:41

decrement counter SL increment by amount

play27:43

we're going to see this in just a moment

play27:45

as we look at the Redux def tools but in

play27:47

the case of asynchronous functions we

play27:49

actually had to Define our name

play27:50

ourselves so we did counter SL increment

play27:53

async this is the first thing and the

play27:55

second thing is in the case of a

play27:57

synchronous actions you always Define

play27:59

the action first using Create async

play28:01

thunk and then you define the reducers

play28:03

when we had synchronous actions we did

play28:05

the reducers first and then we did the

play28:07

actions it's a little bit different just

play28:09

because we're working with asynchronous

play28:10

functions and also the way that you

play28:12

define a reducer for an asynchronous

play28:15

function is different because it doesn't

play28:16

go in this reducers object it actually

play28:18

goes here at the bottom in something

play28:20

that we call extra reducers and this one

play28:23

will take a function what happened my

play28:26

keyboard is Playing Tricks on Me again

play28:28

oh my God this is embarrassing this

play28:30

whole video should be just darus against

play28:32

computer against the keyboard this is

play28:34

going to take a builder as an argument

play28:36

you don't need to worry about this

play28:37

Builder is it's just a tool that is

play28:39

going to allow you to essentially add

play28:41

cases to these reducers and then again

play28:43

let's let co-pilot do its thing great we

play28:46

have builder. ADD case and then here's

play28:48

the magic we have increment async do

play28:51

fulfilled we're getting access to do

play28:53

fulfilled because we used create assing

play28:55

thunk we're also going to get access is

play28:57

the pending and then the error State

play28:59

because when you're working with

play29:01

asynchronous functions you're always

play29:02

going to have different states you're

play29:04

going to have the pending State while

play29:05

that action is running in the case of

play29:07

here while we're waiting for this

play29:09

promise this is going to be in the

play29:10

pending State the promise hasn't yet

play29:12

resolved yet in the case of an API fetch

play29:15

it's the duration that we need to wait

play29:16

for the data to come back from the API

play29:19

and then we have the fulfilled State

play29:20

whenever this promise resolves and we

play29:22

actually get some data back in this case

play29:24

the amount we're going to be in this

play29:26

fulfilled State and as essentially what

play29:27

we've done is we've told this Builder

play29:29

this extra reducers Builder to add a

play29:32

case for the increment async do

play29:34

fulfilled that should run some code

play29:36

again it's going to take the state and

play29:38

an action which is optional and then

play29:40

it's going to make the same updates to

play29:42

the state in the same way that we did

play29:43

them here just slightly different

play29:45

because we're using this extra reducers

play29:47

this in concept literally Works in

play29:49

exactly the same way there's only a

play29:51

little bit of syntax difference just

play29:52

because we're working with asynchronous

play29:54

functions again I'm going to Define here

play29:56

action I'm going to do action and then

play29:58

do a colon and then do payload action

play30:01

and that is going to be of type number

play30:02

as well just so that we know that this

play30:04

payload is a type number so that we can

play30:06

actually add it to the state value and

play30:09

then this works because we pass a number

play30:11

here this returns a number everything is

play30:13

Works no types script is going to

play30:15

complain then what we're going to do is

play30:17

we're just going to add another case for

play30:19

the pending State just so that you get

play30:20

to see how it works this is not actually

play30:22

needed for this action because really we

play30:24

have no use for a pending State however

play30:26

it's really important they understand

play30:28

how this concept Works how asynchronous

play30:29

function work within Redux so we're

play30:31

going to do this what I'm going to do is

play30:33

I'm going to come here and do Builder do

play30:36

add case and that hopefully let's see

play30:38

can co-pilot yes co-pilot knows exactly

play30:41

what we want it is really great let's

play30:42

make a new line here exactly I'm going

play30:45

to remove this comment because we don't

play30:47

need this and then what I'm going to do

play30:49

I'm going to close this parenthesis here

play30:50

I'm going to remove this Builder here

play30:52

and I'm just going to chain these ad

play30:54

cases and save essentially you have one

play30:57

one Builder you can add one case to it

play30:59

and then you can chain another case and

play31:01

you can essentially do this forever and

play31:02

you can chain as many cases as you want

play31:05

so now we have a case for the increment

play31:06

acing pending which again we get access

play31:09

to because we're using this great acing

play31:11

thunk otherwise we will not have access

play31:13

to them and in this one we actually

play31:15

don't even need a state because we're

play31:17

not going to actually do anything to the

play31:18

state so I'm just going to remove this

play31:20

all we're going to do is we're just

play31:22

going to console log increment as sing.

play31:24

pending so that we can see how all of

play31:26

this works so now with this because

play31:28

we've already exported this increment

play31:30

asnc function we can just come here to

play31:32

our counter we can just do import

play31:34

increment async and then instead of this

play31:37

here we can just do increment async and

play31:40

then we can pass it a number it's

play31:41

actually going to give us an error

play31:43

argument of type async thunk this is not

play31:46

configurable to any action correct we

play31:49

forgot to do one thing remember here we

play31:51

had our root State let me just go to its

play31:53

definition we also have ADD dispatch

play31:55

which we haven't used that is because we

play31:57

need to put it here so we're going to do

play31:59

it app dispatch and for this as well

play32:02

save this and now we no longer have an

play32:03

error this is needed if you're working

play32:05

with asynchronous actions so we've

play32:08

dispatched the increment async action

play32:10

again we've passed it this argument

play32:12

which it knows to expect an argument

play32:14

because we've defined it here we said

play32:16

amount is number right so this is the

play32:17

argument that is going to be used and

play32:19

then this argument gets return here as

play32:22

the amount which is then going to come

play32:23

in this action here as the payload which

play32:26

is going to allow us to make the state

play32:28

update to our state so if I save all of

play32:30

this go back to my counter save all of

play32:32

this and then if we go back to

play32:34

application if we also open up the

play32:36

console here just so that we can see

play32:37

what's going on if I press increment we

play32:39

get increment Asing not pending and then

play32:42

exactly 1 second later our state is

play32:44

incremented by 10 let's do it again

play32:46

increment we get the console log and

play32:48

then a second later we get incremented

play32:50

by 10 a third time just to be sure

play32:53

increment console log a second later we

play32:55

get 30 our application our reux

play32:58

application now fully supports

play33:00

asynchronous actions which means that we

play33:02

can do anything that is asynchronous

play33:04

here we have a simple increment Asing

play33:06

but you can do a fetch an API request

play33:09

you can do anything that you want you

play33:11

can have any amount of arguments here

play33:12

you can Define them how you want do

play33:14

whatever you want in this function

play33:16

return a payload and then you get access

play33:18

to this payload here in this reducer

play33:19

Builder and then you can make any State

play33:21

updates that you want super super easily

play33:24

great let me now just show you one last

play33:25

thing this is going to be super short

play33:27

but it's going to be worth it and then I

play33:29

promise you you're never going to have

play33:30

to watch another video on this ever

play33:32

again what I want to show you is the

play33:34

Redux de tools these are really really

play33:36

useful if you're working with redog

play33:38

because it helps you debug a lot of

play33:40

things super easily you can get this for

play33:42

Chrome it's super easy just Google Redux

play33:44

def tools and you're going to get it

play33:45

it's the first link essentially this

play33:47

will automatically connect to your Redux

play33:49

store and then tell you everything that

play33:51

is going on in your application so let

play33:53

me just refresh here so that we have a

play33:55

clean slate essentially if I press

play33:57

increment here let's see what happens we

play33:59

get counter SL increment asyn SL pending

play34:03

and then we get counter SL increment

play34:05

async SL fulfiled these come directly

play34:07

from our reducers here from our actions

play34:09

because we use create async thunk

play34:11

remember we get access to pending and

play34:13

then fulfilled these are a same match

play34:15

and remember how I said that we needed

play34:16

to define the name of this asynchronous

play34:18

function we had to give it here counter

play34:20

increment async well that's exactly what

play34:22

we're seeing here we're seeing counter

play34:24

SL increment async SL pending this

play34:27

pending and fulfilled comes directly

play34:29

from create assing thunk not only that

play34:31

but here on the right if I select your

play34:33

fulfilled you get to see exactly the

play34:35

difference that our state went when this

play34:38

action was done we went from 0o to 10 if

play34:40

I press increment again we get a new

play34:42

pending and this one states are equal

play34:45

there's no difference because remember

play34:46

in this one all that we did is we just

play34:48

did a console log so there's no actual

play34:50

state upat is being done but in the case

play34:52

of fulfilled we went from 10 to 20 you

play34:55

can easily track how all of your updates

play34:57

happen and which actions caus them if we

play34:59

then do decrement you're going to see

play35:01

that the only thing that we have is

play35:03

counter SL decrement now we never

play35:05

actually directly set the name of this

play35:07

action anywhere this is because we use

play35:09

create slice and then we use these

play35:11

reducers here and then we exported them

play35:13

using this using counter slice. actions

play35:15

this is automatically handled for us so

play35:17

you never even have to worry about

play35:19

defining the name of your specific

play35:21

actions reux toolkit is going to do that

play35:24

automatically for you and if I click

play35:26

here you're going to see that our state

play35:27

went from 20 to 19 this is really useful

play35:30

because it helps you debug it helps you

play35:32

figure out what went wrong if something

play35:34

went wrong in your react application and

play35:36

you can easily track which which actions

play35:38

led to which state differences not only

play35:41

that but another really cool feature of

play35:43

these def tools is that you can directly

play35:45

jump to a previous action for example

play35:47

increment acing fulfilled I can press

play35:49

jump here and our state now goes back to

play35:52

the state where this action was done

play35:54

right you can see here the state we went

play35:56

from 0 to 10 10 I directly jumped back

play35:58

in time super easily to see how my

play36:01

application look like at that specific

play36:03

point in time I can click back here and

play36:05

I can jump to the present moment and our

play36:06

state now is 19 with all of these

play36:08

different updates you can jump to any

play36:10

action you can go back in time and

play36:12

forward in time super easily which makes

play36:14

it really really easy to debug if you're

play36:16

working with Redux with toolkit or not

play36:19

this is really invaluable and I would

play36:21

always recommend that you install this

play36:22

because it's really really useful all

play36:25

right guys that was a really long

play36:26

tutorial I really hope that you've

play36:27

enjoyed this that was rux with reux JS

play36:30

toolkit I promised you in the beginning

play36:32

of this video that if you watch this

play36:33

video and you watched it until the end

play36:35

you wouldn't have to watch another video

play36:37

on this ever again and I really hope

play36:38

that I was able to deliver on that

play36:40

promise if you enjoyed this video and

play36:42

you want to see more videos like these

play36:44

you can click here to subscribe it would

play36:45

really help me out a lot you can also

play36:47

click here to watch a different video of

play36:48

mine which I'm sure that it's super

play36:50

super awesome and with that being said

play36:52

my name has been darus causin this is

play36:54

causin Solutions thank you so much for

play36:56

watching and I will see you all in the

play36:58

next video cha cha

Rate This

5.0 / 5 (0 votes)

関連タグ
Redux ToolkitState ManagementReact TutorialActionsReducersAsynchronous OperationsJavaScriptFrontend DevelopmentGlobal StateDebuggingRedux DevTools
英語で要約が必要ですか?