ReactJS Course [6] - Component Lifecycle | UseEffect Tutorial

PedroTech
25 Jul 202219:24

Summary

TLDRThis React.js tutorial delves into the component lifecycle and the powerful 'useEffect' hook. The instructor explains the three stages of a component's life: mounting, updating, and unmounting, using a simple project to illustrate these concepts. The 'useEffect' hook is highlighted for its role in controlling actions based on lifecycle stages, with examples demonstrating its use for API calls and state changes. The video also addresses Strict Mode's role in identifying potential issues with useEffect. The tutorial is designed to provide a foundational understanding before delving into more advanced applications in subsequent lessons.

Takeaways

  • πŸ“š The video is the sixth episode of a React.js course, focusing on the component lifecycle and the use of the useEffect hook.
  • πŸ”„ The React component lifecycle is divided into three stages: mounting, updating, and unmounting.
  • πŸ“ˆ Mounting refers to a component appearing on the screen or starting to exist in a project.
  • πŸ“‰ Unmounting is the opposite of mounting, where a component stops appearing on the screen.
  • πŸ›  Updating is when a component changes the UI in response to changes in props, state, or other factors.
  • πŸ”‘ The useEffect hook is crucial for controlling actions based on the lifecycle stage of a component.
  • 🎯 useEffect can be used to perform actions when a component mounts, updates, or unmounts.
  • πŸš€ useEffect is triggered by default on mount and update, but can be controlled with dependency arrays.
  • πŸ” The video demonstrates the use of useEffect with a simple project involving a text component and a button.
  • πŸ‘€ The use of Strict Mode in React can help identify potential issues by simulating mount and unmount cycles.
  • πŸ”¬ The video explains that useEffect may run twice in Strict Mode as a way to check for proper cleanup on unmount.
  • πŸ”‘ The video emphasizes the importance of understanding useEffect for efficient and clean React component management.

Q & A

  • What is the main focus of the sixth episode of the React.js course?

    -The main focus of the sixth episode is to teach about the React.js component lifecycle and how to use the 'useEffect' hook to manipulate components based on their lifecycle stages.

  • What are the three stages of the React component lifecycle mentioned in the video?

    -The three stages of the React component lifecycle are the mounting stage, the updating stage, and the unmounting stage.

  • What does the term 'mounting' refer to in the context of React components?

    -'Mounting' refers to the process where a component appears on the screen or starts to exist in the project.

  • What is the purpose of the 'useEffect' hook in React?

    -The 'useEffect' hook is used to control what happens depending on which stage of the component's lifecycle it is in, such as mounting, updating, or unmounting.

  • How does the 'useEffect' hook work by default?

    -By default, the 'useEffect' hook executes the function provided to it when the component mounts.

Outlines

00:00

πŸ“š Introduction to React.js Component Lifecycle and useEffect Hook

The video script introduces the sixth episode of a React.js course, focusing on the component lifecycle and the useEffect hook. The instructor aims to explain the conceptual foundation of React components' lifecycle stages: mounting, updating, and unmounting. A simple project is created to demonstrate these stages, using a button to toggle the visibility of a text component, illustrating the lifecycle in action. The script also mentions the availability of the code in the video description and teases the upcoming examples that will clarify these concepts.

05:02

πŸ”§ Deep Dive into useEffect Hook and Lifecycle Stages

This paragraph delves into the functionality of the useEffect hook in React, which is pivotal for managing side effects in components. The useEffect hook is described as a way to perform actions based on the lifecycle stage of a component. The video demonstrates how useEffect can be used to run code upon mounting, updating, and unmounting of a component. The script clarifies the default behavior of useEffect, which is to execute the function it encapsulates after every render, and how to control this with dependency arrays. It also explains the importance of cleanup functions in useEffect to handle unmounting efficiently.

10:03

🌐 Practical Uses of useEffect Hook for API Calls and Component Interactions

The script discusses practical applications of the useEffect hook, such as making API calls when a component mounts or updates. It explains how to use useEffect to trigger actions in response to state changes, and how to limit these triggers to specific dependencies. The paragraph emphasizes the versatility of useEffect, illustrating how it can be used to manage subscriptions or requests that need to be set up or cleaned up in relation to a component's lifecycle. The video promises further examples in the next episode to solidify understanding.

15:05

πŸ› οΈ Strict Mode and its Role in Detecting Lifecycle Issues

The final paragraph of the script addresses Strict Mode in React, a tool designed to help developers write better code by highlighting potential issues. The instructor explains that Strict Mode can cause components to mount and unmount twice, which is a test to ensure useEffect is implemented correctly and to avoid memory leaks. The script suggests that Strict Mode should always be used during development, as it prevents anti-patterns and promotes best practices. The video concludes with a note on the upcoming content, which will include exercises and more in-depth examples of useEffect in action.

Mindmap

Keywords

πŸ’‘React.js

React.js is an open-source JavaScript library used for building user interfaces, particularly single-page applications. It is the main focus of the video, where the instructor discusses the component lifecycle and 'useEffect' hook within the context of React.js development. The script mentions it as the subject of the course and the framework in which the examples are built.

πŸ’‘Component Lifecycle

The component lifecycle in React.js refers to the stages a component goes through, from its creation (mounting), to updates, and eventual removal (unmounting). The script explains the importance of understanding these stages to write efficient React components and uses the lifecycle to illustrate how the 'useEffect' hook operates within these stages.

πŸ’‘useEffect Hook

The 'useEffect' hook is a feature in React.js that allows developers to perform side effects in functional components. The video emphasizes its importance for manipulating components based on their lifecycle stage. It is used to run actions when a component mounts, updates, or unmounts, which is demonstrated with examples in the script.

πŸ’‘Mounting

Mounting is the first stage of a React component's lifecycle, where the component is inserted into the DOM. The script uses the concept of mounting to explain when certain actions within the 'useEffect' hook should be triggered, such as making an API call when a component is first rendered.

πŸ’‘Updating

Updating is a stage in the component lifecycle where a component re-renders due to changes in state or props. The video script illustrates updating by showing how the 'useEffect' hook can be configured to respond to state changes, thus allowing components to update in response to user interactions.

πŸ’‘Unmounting

Unmounting is the final stage of a component's lifecycle, where it is removed from the DOM. The script explains how to use the 'useEffect' hook to execute cleanup logic when a component unmounts, which is crucial for preventing memory leaks and ensuring the application's efficiency.

πŸ’‘State

State in React.js is an object that represents the part of the component that can change. The script discusses state in the context of updating components, showing how changes to state can trigger the 'useEffect' hook and cause components to re-render.

πŸ’‘Props

Props, short for properties, are the mechanism by which data is passed from a parent component to a child component in React.js. The script mentions props as a potential trigger for the 'useEffect' hook, indicating that changes to props can also cause components to update.

πŸ’‘Strict Mode

Strict Mode is a React feature that helps developers to identify potential problems in their code. The script explains that Strict Mode can cause the 'useEffect' hook to appear to run twice, as it forces a mount and immediate unmount to check for issues, which is an important aspect of debugging React components.

πŸ’‘Memory Leak

A memory leak in the context of React.js occurs when a component continues to consume memory after it has been unmounted, leading to performance issues. The script uses the 'useEffect' hook's return function to demonstrate how to prevent memory leaks by cleaning up subscriptions or intervals when a component unmounts.

Highlights

Introduction to the sixth episode of the React.js course focusing on the component lifecycle and the use effect hook.

Explanation of the React.js component lifecycle, including the mounting, updating, and unmounting stages.

The importance of understanding the lifecycle stages for efficient component management.

Demonstration of a simple project to illustrate the lifecycle stages in action.

Use of the 'show text' state and button to toggle the visibility of the 'text' component.

Real-time updating of the component state through user input in an input field.

The use effect hook as a crucial tool for controlling component behavior based on lifecycle stages.

Default behavior of the use effect hook triggering actions on component mount.

The use effect hook's ability to execute actions during component updates based on state changes.

Specifying which state or props changes should trigger the use effect with an array.

Executing cleanup actions when a component unmounts by returning a function from the use effect.

The practical use of use effect for API calls and continuous requests while a component is mounted.

The role of strict mode in React for helping developers write better and more efficient code.

Explanation of the double invocation of use effect in strict mode for code correctness checks.

Recommendation to keep strict mode on to avoid potential memory leaks and code anti-patterns.

Upcoming episodes will feature more advanced use cases of the use effect hook.

Invitation to subscribe and support the channel for more React.js course content.

Transcripts

play00:00

hey guys how's it going i'm back here

play00:01

with another video and today this is the

play00:03

sixth episode of my react.js course in

play00:05

this video we're gonna be learning

play00:07

really important stuff the first thing

play00:09

we're gonna go over is um a lot of

play00:11

conceptual stuff related to what exactly

play00:13

the react.js component lifecycle is and

play00:16

then i'm going to explain to you guys

play00:18

how to use the use effect hook to

play00:20

manipulate components depending on where

play00:23

in their lifecycle stage they are right

play00:25

so i'm going to go over all of this in

play00:27

the single video and i'm really excited

play00:29

to show you guys the examples if you're

play00:30

interested in checking out the code for

play00:32

the video it's going to be all in the

play00:33

description and yeah that's basically it

play00:36

so let's get into the video

play00:38

[Music]

play00:39

[Applause]

play00:41

[Music]

play00:45

[Applause]

play00:48

okay everyone so in order to start

play00:50

explaining how life cycle works in react

play00:53

and how um the use effect hook works in

play00:56

react as well i want to first uh i

play00:58

created this example over here

play01:00

that is going to be able to explain to

play01:02

you guys exactly how everything works

play01:04

but i also want to give you guys some

play01:05

terminology before we start working with

play01:07

this so life cycle like the word

play01:10

says it's basically a description of how

play01:13

a component uh like the collide check

play01:15

over component just explains how uh

play01:18

everything happens from the birth of a

play01:19

component to the death of the component

play01:21

obviously that's an analogy but um

play01:23

everything that happens from the

play01:24

beginning till the end so there's three

play01:27

different stages

play01:29

in the react life cycle there's the

play01:31

mounting stage there's the updating

play01:34

stage and there is the unmounting stage

play01:38

so the important word to know here which

play01:40

you might not know is mounting and

play01:41

mounting basically uh just think of

play01:44

mounting as the component appearing in

play01:46

the screen or the component um starting

play01:48

to exist in your project um and then

play01:50

mounting being the opposite of that so

play01:52

the components stop appearing in the

play01:53

screen so in

play01:55

in a website uh that would be the an

play01:58

element a component that represents a

play02:00

bunch of

play02:01

html elements or jsx elements

play02:04

suddenly appearing in the source code

play02:06

and then unmounting would be basically

play02:09

that disappearing and then updating you

play02:12

guys might know what the word means it

play02:13

just means that um that component it is

play02:16

changing the ui and and whatever it's

play02:18

returning inside of the component is

play02:20

changing depending on something

play02:22

such as for example a prop has changed

play02:25

or a state inside of the component has

play02:27

changed or something like that right

play02:29

so those are the three words that we

play02:30

need to understand because those are the

play02:32

three stages of a react component life

play02:34

cycle now

play02:36

we can see this

play02:38

existing and working very easily by

play02:40

creating a simple project that showcases

play02:43

this so there's many different projects

play02:45

that you can just create to to showcase

play02:47

that

play02:48

this one over here i find it to be

play02:50

pretty simple and it just uses stuff

play02:51

that we've done in the previous episode

play02:53

so i find it to be really cool um

play02:55

basically we have over here uh our app

play02:58

component and inside of it we have um a

play03:01

state called show text which is just a

play03:04

boolean true or false and a button which

play03:06

when we click on it it should change the

play03:09

value of the state to the opposite of

play03:10

what it is right now so if it's false

play03:12

and we click on the button now it will

play03:14

become true and the state is used to

play03:16

determine if this other component called

play03:19

text

play03:20

is being is appearing on the screen or

play03:21

not now this other component called text

play03:23

is pretty simple as well all it has is

play03:25

this other state called text which is a

play03:28

string

play03:29

and

play03:30

we're showcasing it on the screen over

play03:32

here inside of a header tag but every

play03:34

time we type on an input

play03:36

it will change the value of that text

play03:39

and you can see this working over here

play03:41

because um basically we have the show

play03:43

text button and show text state is false

play03:47

when i click on this the text component

play03:49

appears and i can start um changing the

play03:52

value of the text

play03:54

state right and if i want to basically

play03:57

turn it off i can just click control

play03:58

text again and it will disappear

play04:01

now you can easily see here the three

play04:03

stages of a react component life cycle

play04:05

because if we inspect element which is

play04:08

something i would recommend you guys get

play04:09

really familiar with because

play04:11

it helps a lot with any kind of web

play04:13

development not just react um and we

play04:16

open our screen over here we try to

play04:18

navigate through our app we see right

play04:20

now inside of the div with the class app

play04:23

which is

play04:24

where our whole app exists there's only

play04:27

a button right

play04:28

so this stuff over here all of this ui

play04:31

over here is not currently in our code

play04:34

and that makes sense because it's not

play04:36

being shown right but when i click on

play04:38

this now this div appears so this div

play04:41

represents the text component which

play04:44

means that right now what happened is we

play04:46

just mounted this component into our

play04:48

project so this component was not

play04:50

existing inside of our project or inside

play04:52

of our

play04:54

instead of our web page and now it is

play04:55

existing now if we open this up you can

play04:58

see there's an input and there's an h1

play05:00

tag the h1 tag is obviously empty

play05:02

because the text state is empty

play05:03

initially and the input is over here

play05:06

just chilling nothing is happening to it

play05:07

so what we can do now is we can start

play05:09

typing on this and you'll see

play05:11

immediately that the code inside of this

play05:14

component over here is changing right

play05:17

it's updating in real time because we

play05:19

are updating our component so this is

play05:22

the second stage of the react life cycle

play05:24

you can see it is literally updating as

play05:26

we go now we want to see the last stage

play05:28

right so unmounting if we click on this

play05:31

over here it's going to go back to what

play05:33

it was before now we don't have that

play05:36

div anymore which means we don't have

play05:38

that component anymore now this is

play05:40

extremely important to understand

play05:42

because in a lot of times you want to

play05:44

make your components as efficient and um

play05:47

make them just work exactly how you want

play05:49

them to work so understanding um how a

play05:52

component exists and and the life cycle

play05:54

stages of it is extremely important and

play05:57

there is one specific concept that will

play06:00

encapsulate all of that

play06:02

in react and it's called the use effect

play06:04

hook

play06:05

so

play06:06

back in the days there was actually

play06:08

three different methods that you might

play06:10

have seen if you're digging up react

play06:12

code you might have seen something like

play06:14

component did

play06:16

mount something like this

play06:18

ignore this kind of

play06:19

stuff if you see it in the internet

play06:21

that's all like old react that's when

play06:24

class components was a thing um so if

play06:26

you see that don't worry you don't need

play06:28

to learn that what you need to learn is

play06:30

that there is a hook in react called the

play06:33

use effect hook this is one of the most

play06:36

important hooks in react in my opinion

play06:38

it is the second most important one

play06:39

before the use state although i can

play06:42

argue that you can have a project

play06:43

without a u-state hook but you cannot

play06:45

have a project without a use effect hook

play06:47

so the use effect hook is a hook that is

play06:50

used

play06:51

to basically control what happens

play06:53

depending on which stage of your

play06:56

component's life cycle it is so with

play06:58

this hook over here the simple function

play07:00

over here we can we can

play07:02

create some sort of action that will be

play07:04

triggered when the component is mounting

play07:06

or when the component is updating or

play07:08

when the component is

play07:10

unmounting so that's something extremely

play07:13

cool

play07:14

because it all exists um and you can do

play07:16

all that inside a single um hook inside

play07:19

of single function

play07:20

so i know that might seem a little bit

play07:22

confusing if this is the first time

play07:23

you're working with a use effect so i'll

play07:25

give you guys a very easy example just

play07:27

continuing with whatever we have over

play07:29

here and i'll show you guys exactly what

play07:31

i mean so i'll come over here and if you

play07:33

want to use the use effect hook all you

play07:35

have to do is you just have to say use

play07:37

effect

play07:38

and just open and close parentheses and

play07:41

then put a function inside of the use

play07:43

effect so like this just a function that

play07:46

exists inside of this now the use effect

play07:49

by default what it does is whatever you

play07:52

put inside of here as of right now

play07:54

it will call for example console.log a

play07:57

message over here saying

play07:58

component

play08:00

mounted

play08:02

like this component mounted and when the

play08:05

component mounts

play08:06

this will be console logged that's all

play08:08

it will do right now you'll see that

play08:11

this happens because if i refresh my

play08:13

page go to my console and i click on

play08:15

this button it will say component

play08:18

mountain

play08:19

now there's a thing you might be

play08:20

wondering why did it console log twice

play08:23

now if you watch tutorials about the use

play08:25

effect before

play08:26

early this year which is 2022 you might

play08:29

know that people might say that this

play08:31

only console log once it's because react

play08:33

updated i don't want to get too in-depth

play08:36

about what happens here but i want to

play08:38

emphasize that because that can confuse

play08:40

a lot of beginners what's happening here

play08:42

is

play08:43

now what react does is if you have this

play08:46

thing over here called the strict mode

play08:48

it is supposed to help you write better

play08:50

code that's the whole point of it so if

play08:52

you have that currently in your project

play08:55

it will do it will console log twice um

play08:58

and you see i just removed it and it

play08:59

will only console log once um because

play09:01

what react does is it's basically

play09:03

checking to see if you're handling your

play09:05

use effects correctly again i won't

play09:08

explain that right now i'll explain it

play09:09

at the end of the video for only for

play09:11

those who are interested it's actually a

play09:13

pretty interesting topic but for now

play09:15

just imagine that the use effect

play09:18

only runs once let's just imagine that

play09:20

i'll actually keep this console logged

play09:22

because

play09:23

it's better for for you to understand it

play09:25

this way

play09:26

so

play09:27

as of right now the use effect only is

play09:30

calling itself when the component has

play09:32

mounted and you'll see that

play09:34

i need to save this like this i need to

play09:38

do this so as of right now you can see

play09:40

that when the component mounts it says

play09:42

component amounted i will um i'll

play09:44

unmount it again and then i'll mount it

play09:46

again and you'll see it will contour log

play09:48

again so the user effect right now is

play09:50

allowing us to execute an action

play09:53

exactly when the component mounts which

play09:54

is something we have never done before

play09:56

so what what kind of action could we put

play09:59

over here well in the beginning this is

play10:01

very useful for when you for example

play10:03

have a page where you need to make an

play10:04

api call to get data from somewhere

play10:08

immediately when you go into the web

play10:09

page you just put it over here and

play10:11

what happens is when you go into that

play10:13

specific page it will fetch the data

play10:16

immediately and display in the screen

play10:17

that's a very very easy example on why

play10:21

you might want to do something like this

play10:22

right why you might want to execute some

play10:24

sort of action when the comp when the

play10:26

component appears or when the component

play10:28

mounts now as you can see over here this

play10:30

is as it is right now it's only

play10:32

executing an action on one of the three

play10:34

stages of a react component's life cycle

play10:37

so how would we make it so that we can

play10:40

console log when it's updating and we

play10:42

can console log when it's unmounting we

play10:43

want to see if we can actually detect

play10:46

and trigger some sort of action when

play10:48

those two stages are occurring so for

play10:50

actually updating it's actually pretty

play10:53

simple so with the use effect over here

play10:55

this will be called every single time

play10:59

there's a state change so what i mean by

play11:01

that is whenever this component

play11:03

re-renders

play11:05

whatever is inside of here will be

play11:06

called again so what happens is if the

play11:08

component itself updates in any way it

play11:12

can either be a prop that is receiving

play11:14

changes or a state inside of it like

play11:16

this one over here changes then this

play11:18

will be called again so let's see if

play11:21

that actually happens so you'll see it

play11:23

will be called once because it mounted

play11:25

and now if i change a state inside of

play11:27

this component it will keep calling

play11:29

itself

play11:30

meaning that

play11:32

over here

play11:33

whatever we put inside of here

play11:35

as it is right now will also be called

play11:38

when the component is updating so the

play11:40

two stages are now being triggered

play11:42

now there's a caveat what if we only

play11:44

want a console like this if it's mounted

play11:46

but not when it's updating well the use

play11:49

effect hook allows us to put an array

play11:51

over here

play11:52

and this array basically tells us we can

play11:55

specify what props or state changes we

play11:58

want

play11:59

to trigger the use effect so if i put

play12:02

the text over here

play12:05

then what will happen is the same thing

play12:06

would happen right

play12:08

it will console log and trigger the use

play12:11

effect hook whatever is inside of here

play12:13

every time the text changes so the same

play12:14

thing was happening but

play12:17

if i remove the text from here

play12:19

you'll see that now

play12:21

it doesn't trigger it anymore

play12:23

only the mounting triggers so if you

play12:26

want to execute an action only once when

play12:28

the component mounts you put an empty

play12:30

array over here but most importantly if

play12:33

you want to trigger an action every time

play12:36

a specific state changes or many

play12:38

different state changes you can just put

play12:40

them

play12:40

over here for example putting the text

play12:43

over here this is really cool and it

play12:46

comes in handy in so many situations so

play12:48

it is extremely important for you to

play12:49

understand how this works however i know

play12:52

that since this is the first time you're

play12:53

probably seeing the useful tuck it will

play12:55

be hard to come up with examples in your

play12:57

head and see the how useful this is but

play13:00

don't worry i have that in mind we're

play13:02

going to be using this a lot especially

play13:03

in the next video now we've done two

play13:06

different stages what is the third stage

play13:08

of a react component's lifecycle well

play13:11

it's unmounting how do we execute an

play13:13

action when the component disappears so

play13:17

with a use effect we can return a

play13:19

function

play13:20

and this function over here will be

play13:23

executed when the component amounts so

play13:26

if we want to console.log for example

play13:29

something saying component

play13:31

unmounted like this so now if we remove

play13:35

this over here i want to remove it just

play13:37

so you guys can

play13:39

see this working better

play13:41

what will happen is

play13:42

immediately when we show the text the

play13:44

component it should console log that the

play13:46

component was mounted and if we decide

play13:48

to not show it anymore it will contour

play13:50

log that the component was unmounted so

play13:52

i'll come over here refresh the screen

play13:54

show the text it will say component

play13:56

mounted and then

play13:58

remove it and you'll see you'll see that

play13:59

it says component unmounted so we are

play14:02

triggering both things immediately so

play14:05

how can this be useful there's many

play14:07

different situations imagine that you're

play14:10

you want to make a i don't know a

play14:12

request to a service an api

play14:15

when you render a component so when a

play14:17

component mounts you want to make a

play14:19

request for an api and continuously make

play14:21

that request but then when the component

play14:23

disappears you want to stop that request

play14:25

well you can put the the logic for

play14:27

making the request over here and put the

play14:29

logic for stop like the logic for

play14:32

stopping to make that request over here

play14:34

so this serves for you to kind of clean

play14:36

up um the mass you made um not the mess

play14:39

you made but like whatever logic you put

play14:41

over here if it's a logic that might

play14:43

cause leaks after this you you might

play14:46

want to fix it over here

play14:47

so that's basically it for

play14:50

the use effect hook now again i know

play14:53

that um

play14:54

it might not seem that useful right now

play14:56

because i'm just using console logs but

play14:58

the next episode is where i show you

play15:00

guys the first major example of using

play15:03

the use effect inside of your project it

play15:05

is

play15:05

my opinion the main one you use in the

play15:07

beginning um afterwards you'll be using

play15:10

this all the time when you get more

play15:11

advanced in react but um i just needed

play15:14

to make this video first so you guys

play15:15

could have an idea

play15:17

now i want to explain to you guys the

play15:19

whole uh streak mode thing so if you're

play15:22

not interested in that you can just go

play15:23

to the next episode if you're interested

play15:25

i'll go over why um this thing makes the

play15:28

request in the use effect twice or makes

play15:30

the use effect run twice

play15:32

so stick around for that so basically we

play15:35

have over here what is known as strict

play15:37

mode so i mentioned to you guys that

play15:39

strict mode over here is a way for react

play15:41

to kind of

play15:43

help us write better code right so what

play15:45

do i mean by that well react has certain

play15:47

roles um inside of their project that a

play15:49

lot of people like not following

play15:52

and streak mode will make some checks

play15:54

for you um while you're writing your

play15:56

code and prevent you from making those

play15:58

mistakes i would always recommend

play16:00

keeping this on because if you're make

play16:02

if you're writing some code that um

play16:04

strict mode is not allowing you to you

play16:06

should probably not be writing that code

play16:08

um so just removing it is kind of

play16:10

redundant because you're literally doing

play16:12

something that is an anti-pattern

play16:14

something that react doesn't recommend

play16:16

and it doesn't matter what use case you

play16:18

have

play16:18

you're running into there's always a

play16:20

solution and strict mode is just telling

play16:22

you that the one you have right now it's

play16:23

not the best one so that's a little bit

play16:25

of a rant for those who don't want to

play16:27

use trick mode in my opinion you have to

play16:29

and what happens over here with the

play16:32

component mount

play16:33

running twice is that

play16:35

if i go over here right now into our

play16:37

project right with strict mode and i

play16:39

show the text you'll see that what

play16:41

happens is the component mounted once

play16:44

the component unmounted right after it

play16:46

and then the component mounted again

play16:48

so

play16:49

that's something interesting to see

play16:50

right

play16:52

we thought it was running twice but what

play16:54

is really is happening is with strict

play16:56

mode react forces the component to mount

play16:59

once then unmounts immediately after and

play17:02

then mounts again so

play17:04

that's just to check to see if your user

play17:08

fact is running correctly because if you

play17:11

messed up over here like i said you

play17:13

messed up and you wrote some code that

play17:14

can cause some leak so if you wrote some

play17:16

code over here that can cause some type

play17:18

of leak or some some type of memory leak

play17:20

then you react you will know because uh

play17:23

what happens is uh react will force the

play17:25

component to unmount meaning the leak

play17:27

will will become evident and then it

play17:30

will mount again

play17:31

now what happens is if your code is

play17:34

running correctly meaning that whatever

play17:37

logic you put over here you're cleaning

play17:39

up on the unmounting stage then it

play17:41

shouldn't have any issues with it

play17:43

because what it's going to happen is

play17:44

it's gonna mount for a sec for like a

play17:46

small amount of time then immediately

play17:48

unmount so if your component is running

play17:50

correctly um everything should remain

play17:52

the same and then it's gonna run mounts

play17:54

again to actually run the use effect

play17:57

like you intended to so this is just a

play17:59

way to check to see if you wrote your

play18:00

code correctly if you didn't then

play18:02

immediately you'll see everything will

play18:04

be breaking there's infinite examples of

play18:06

this but um like i said since this is

play18:09

the first time you guys are learning use

play18:10

effects um it might get kind of

play18:12

confusing if i just went on a rant about

play18:15

um

play18:15

different use cases of this when you

play18:17

guys might have never used the use

play18:19

effect before this video so

play18:21

um this will become really more evident

play18:23

as we go on to the series there's

play18:24

actually a really cool video um from

play18:26

another youtuber which goes over just

play18:28

this specific topic um

play18:31

meaning the the use effect calling twice

play18:34

i'll prob i'll probably tag it up here

play18:35

on the video if you're interested in

play18:36

checking it out um it's really

play18:38

interesting so with that in mind this

play18:40

was it for the video there's no exercise

play18:42

in this video it's the only video with

play18:44

no exercises because it's hard to come

play18:46

up with an exercise for the user effect

play18:48

when we haven't learned stuff like api

play18:50

requests or or something like that but

play18:53

keep in mind that's going to be in the

play18:54

next few videos so i really hope you

play18:56

guys enjoyed it if you enjoyed it please

play18:58

leave a like up below and comment what

play18:59

you want to see next subscribe because

play19:00

i'm posting two to three times a week

play19:02

and i'm really enjoying this course so i

play19:04

would massively appreciate if you guys

play19:05

could help support the channel and i see

play19:06

you guys next time

play19:09

[Music]

play19:09

[Applause]

play19:12

[Music]

play19:20

[Applause]

play19:22

[Music]

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
React.jsuseEffectComponent LifecycleCoding TutorialWeb DevelopmentJavaScriptMounting StageUpdating StageUnmounting StageStrict ModeAPI Requests