Complete React Tutorial (& Redux) #38 - Store Subscriptions
Key Takeaways
Explains how to subscribe to changes to the state and react to them
Full Transcript
or rather than so if we take a quick look at this diagram again then you're going to notice we've done most of the steps here we've created a store and we've created a reducer which is passed into that store we've also dispatched an action right here which is going into the reducer the reducer is taking that action and then updating the state in the store so the only real thing left to do is to subscribe to the store and then react to changes in the store now normally at what we do is do that in a component in react but since we don't have any components here I'll just do a simple subscription and log it to the console whenever we get a change but it will be slightly different when we work with components but the basic idea behind it is the same we want to listen for changes to the store and then react to those changes so then let's set up these subscriptions so down here underneath the store what we're going to do is say store dots subscribe and this is a function and it takes a function as a parameter and this function is get a fire every time the state is changed so every time that happens let's console.log state update it and then underneath that will do another console log and we'll logout the state now how do we access the states we can't just say state because that's the thing that's just passed in here and if we say initial state then we're just going to grab this thing right here we want the current state of the store which has been updated so we can get that by saying store don't get States and this is a function and that's going to get the state for us and love that's in the console now all right so if we save this and run it let's see what is produced in the console over here all right so now we see we get that state which is two dews by milk so this is working we've dispatched the action right here like this and it's passed into the reducer we've updated the state we've passed back a new state object and then because the state has changed and we subscribe to that then it's firing this function over here and it's logging this message and the state over here vault we've got a problem in the state here is an object with just one property - duze so we have two properties - dues and posts even though this one's still empty it should still be there now the problem lies here when we return an object this is the new state object so we've overridden the old one in essence so we said okay we no longer want posts here right so this is different from when we're setting the state in a react component whereby we only need to pass through the thing that we update and it doesn't touch the rest in this case what we need to do is pass in even the stuff that we don't want to change so we could say posts is this an empty array still and then sadducees update it we could do that but what if you have many different properties on here well you don't want to tap them all out for each individual check so instead what you can do is use the spread operator of the state which is going to grab everything in the current state so both of these things and it's going to spread them in here then it's gonna take their - dues and actually override that so we might be doing to do twice here but the second one is the one that counts the last one in the chain that overrides the first one okay so this way we're still placing the posts in the state object that we returned but we're also overriding the - dues so if we save this again and press run this should work fingers crossed and voila we get it right there all right there so let's now try dispatching another couple of actions so I'm gonna go down here I'm gonna get rid of these two things and what I'm gonna do is just paste in a couple of actions so stored up dispatch and then I'm passing the action directly into the dispatch right here so the first one is absolu by milk the same as before the second one is add to do again and this time sleep some more now we'll do a third one so I'll just copy one of these and this time it's gonna be add post and it's gonna be egg hunt with Yoshi all right so now we need to perform this check inside the reducer over here so copy this and we want to paste it down below here and I'm going to say if action type is now add post then we want to do something different don't we we want to return an object which takes the current state but this time we want to update the posts so let's do that and we want to use the spread operator on the current posts and then also get the post data and let's update that on the action down here because that's going to be a post and not to do now so let's save this now I'm going to clear the console over here save it and see if this works so we'll run this and we should get a few logs out here the first one we've added it to do the second one we've added a new to-do and the third one we've added a post so that's the basics of Redux right there just to go through the diagram again we dispatch an action to change the state and that is passed into the reducer the reducer takes that action checks what type of action it is and then updates the state accordingly with whatever day two it's passed then we can subscribe to the store and react to that in this case we're just logging something to the console but when we come to work with react and redox we're going to attach that data to the props of this component so we can use them and we're going to see that in the coming videos
Original Description
Hey gang, in this React & Redux tutorial I'll explain exactly how we can subscribe to changes to our state and react to them.
----------------------------------------
🐱💻 Course Links:
+ VS Code editor - https://code.visualstudio.com/
+ GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist
+ React CDN - https://reactjs.org/docs/cdn-links.html
----------------------------------------
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI