Complete React Tutorial (& Redux) #8 - Changing State (and 'this')
Skills:
React90%
Key Takeaways
Discusses the context of the 'this' keyword and updating component state in React
Full Transcript
alright then so in the last video we saw that when we try to access this state inside our functions in reaction to Dom events in the window in the browser then this was undefined and we want to know why that is because we can access this dot state down here in the template but not up here inside the functions and that's to do with the value of the this keyword in these two different locations now in JavaScript the context of the this keyword inside a function is generally determined by how and where the function is called not by where the function sits okay now if you're not aware of this then sometimes the value of the this keyword inside a function is not what you'd expect so he would probably assume that the context of the this keyword inside one of these functions would point to our react component instance and would be able to access this dot state like in the template but by default when we call a function like this in reaction to a Dom events in the browser window like this click here the context of the this keyword is lost inside the function and it's set to undefined which is why we get undefined and an error in the console of a hit and when we try to do this ok so that there is the normal behavior of JavaScript there's no need to blame react for this now old versions of react bound the context of the this keyword right here to the component instance in our functions it did that for us in old versions but in the new version of react when we use class components like this then it doesn't do that for us anymore and we have to manually bind the context after this keyword to our functions now inside the render method down here we can access this state so why is that different well that is a built-in react function right and react automatically binds the context of this forest inside this function so we don't have to do any manual work there however inside own custom functions of this class when we tell them in reaction to Dom events the context of this is not bound so we have to do it ourselves now there's a couple of different ways to do this but the easiest way is very simple and it's just to use an arrow function for our methods up here instead of normal functions like this so for example if I set this equal to an arrow function like so then this will work and I'll demonstrate that now so if I save it and press click me then this now works we can log this dot state to the console but why is that well arrow functions right here they have a special behavior when it comes to this keyword they bind the value of this to whatever this is outside the function so whatever the value of this is right here and the value of this right here inside this class component is just a component instance so it's taking that value and it's binding it into this right here okay so when we use an arrow function it takes the value of this to be the component and passes it inside the function so now we can do that so that's the easy way to do this just change these two arrow functions like so and the value of this inside those arrow functions is now going to be equal to the component instance so we can access the state like that now if all of this went over your head don't worry too much about it you just have to remember that we define our custom functions inside components this way as arrow functions when we want to use this inside them and access or change the state now if you do want to read more about that this keyword in react there's a fantastic article which I'm going to leave the link to down below so you can go ahead and read that so now we know how to use this inside the functions we can see we can access the state this way and we saw that in the console over here we can log it over there now what if we want to change the state in reaction to some kind of event when we click on this I want to change the name to something else to Yoshi how do we do that well we don't just say this dot states that name is equal to yo should we don't do that that is bad practice don't change the state directly like that however we can say this dots set states and this is a function so this function is used to change the state and in here we pass an object and that object represents the states so what we do is pass in whichever properties we want to update we don't have to pass in them all we can just pass in one and that's going to update just that one property but if we want to we could pass in both of the properties or if there was a third property or three so I just want to update the name and I want to change that to Yoshi so this is gonna update the state for us and just change the name property it's gonna leave aged as it is so let's give this a whirl save it and if I come over here and click the button you can see now this has changed and it says my name is Yoshi and I'm 30 if we change the age as well set that to 25 save it click me and Yoshi and I'm 25 awesome so that my friends is the this keyword in react functions and how we can change or access the state inside our components
Original Description
Hey gang, in this React tutorial we'll be discussing the context of the 'this' keyword inside our component functions, as well as looking how to update the state of our component.
More on THIS:
https://medium.freecodecamp.org/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb
----------------------------------------
🐱💻 Course Links:
+ VS Code editor - https://code.visualstudio.com/
+ GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-8
+ 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
More on: React
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Dev.to · Etrit Neziri
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
🎓
Tutor Explanation
DeepCamp AI