React Context & Hooks Tutorial #12 - useEffect Hook
Key Takeaways
Introduces the useEffect hook in React Context API with Hooks
Full Transcript
okay then so now we've seen the first hook use state I want to move on to the next hook and that one is called use effect so let's first of all import that up here use effect and all hoaxes pretty much just use this naming convention use something or other so you can think of this use effect hook as a bit like a lifecycle method that we'd normally use inside a class component in react so when we use functional components when we don't have access to those lifecycle methods and if we wanted to run some code when the component updates for example then we'd normally need to use a class component and hook into a lifecycle method now though we can use this use effect hook instead in functional component to do just that so this use effect hook let's just use it down here use effect is a function again and it takes in here a callback function as a parameter and that callback function is going to run every time the component renders or re-renders ie every time the data inside our component changes and also on the initial render so typically you could use this use effect hook to do something like communicate with a database or an API endpoint or something like that for now they'll let me just do a very very simple example all I'm going to do is log something to the console so we'll say console dot log and we want to say use effect hook run and then we'll say we want to output the songs as well so if I save this now hopefully when it first loads and it first renders we should see this function fire and then every time the data here changes inside the component then the component will rerender and on that re-render this function is going to fire again every time the component renders so let me save it and preview over here so we can see use effect hook run and we have the data right here because we logged that to the console as well if I add a new song then we should see it again in a second ignore this D that's just me typing it for some reason anyway now we see use effect hook run again this time with the extra data it would just type in some random stuff there again then we can see use effect hook run again okay so you get the gist this is running every time the component renders or every time the data updates inside the component and when it first renders as well so now then imagine this imagine we had two lots of different data inside this component and then anytime any of that data is updated then the hook is going to run right so let me demonstrate this for example just bear with me I am going somewhere so this is not all going to be pointless well let me now create some more states by using the you state hook and we'll say Const and this will be UPS age and set age and we'll set that equal to use state again and we can do this we can use use state as many times as we want inside a component and we're just storing different values basically like this one is songs and set songs this one is age and set age so I'm going to initialize this to 20 to begin with then I'm going to create a button and when we click on that button it's going to increase the age by one so let's do that at the bottom down here we'll do a new button and inside this button we'll just say add 1 to age and then we'll also output the age so curly braces and age remember that's what we called age right here so to begin with this will be 20 right here but as we click on this I want to start adding to this so let's add the on click event handler and inside we'll just do an inline RL function and we want to call set age and we'll take the current age plus 1 so basically it the new value is going to be whatever the current age is plus 1 and that should then update the state therefore we get a rerender and therefore this should run as well right so let me now save this and preview over here so we can see it's currently 20 add 1 to age it goes to 21 and you see use effect hook run do it again and it comes again so this data is not changing the songs but that doesn't matter some data inside the component is changing therefore it's rear-end during the component and it's running that use effect hook or the cob up function inside that hook now we might not want to run this callback function every time any data is updated imagine we only wanted to run this function for example when this data changed and not this data how would we do that well if we come over here we can see I mean it still runs when we add some data because it's running after any kind of data change all we need to do is limit it so that only when we change the song data it updates and not this so we can do that by passing a second parameter into the user fact hook and that second parameter is going to be an array of data that we essentially want to watch and we only want to run this car bat function when the data inside this array changes so for example I could pass in here songs and that means that what we only want to run this callback function in the use effect hook when the songs data changes and not when the age data changes so if I save this now and come over here if I start to add one to age then notice it's still increasing the age the date is changing the component is re-rendering but this is not running all the time we only get it on the initial render so now if I start to add songs then we get that callback function so that's nice we're limiting when we use this callback function okay so let's imagine now we want to use some kind of callback function when this changes well we can use use effect as many times as we want inside the component so let me just copy this and paste it down here and I'm just going to change this one now to age so that means only run this callback function when age is changed up here and instead of songs will output age right here so if I save this now and come over here now if I start to add some stuff over here first of all by the way we see two callback functions have run one for the songs and one for the Aged because they both run on initial render nothing to do with adding new dates yet but if we add a new song then we get this one for the songs but we don't get the other if I start adding once age then we get the other combat function running so there we go that's how we use this hook use effect to run some code every time the data changes or when the componentry renders and this is how we limit it to only run based on certain data so now I think that's pretty much all the basics of hooks covered so next I'd like to move on to using hooks alongside context back in our context up over here
Original Description
Hey gang, in this React Hooks tutorial we'll take a first look at the useEffect hook.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/react-context-hooks
+ Complete React Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG
+ React Context Docs - https://reactjs.org/docs/context.html
+ Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT
🐱💻 🐱💻 Other Related Courses:
+ Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB
Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
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