Lifecycle Hooks

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€60.11
List Price:  €85.87
You save:  €25.76
£52.07
List Price:  £74.39
You save:  £22.32
CA$96.72
List Price:  CA$138.18
You save:  CA$41.46
A$107.37
List Price:  A$153.39
You save:  A$46.02
S$90.24
List Price:  S$128.92
You save:  S$38.68
HK$545.84
List Price:  HK$779.80
You save:  HK$233.96
CHF 56.34
List Price:  CHF 80.49
You save:  CHF 24.15
NOK kr706.74
List Price:  NOK kr1,009.67
You save:  NOK kr302.93
DKK kr448.69
List Price:  DKK kr641.01
You save:  DKK kr192.32
NZ$119.45
List Price:  NZ$170.65
You save:  NZ$51.20
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,511.37
List Price:  ৳12,159.63
You save:  ৳3,648.25
₹6,169.35
List Price:  ₹8,813.74
You save:  ₹2,644.38
RM295.88
List Price:  RM422.70
You save:  RM126.82
₦106,874.73
List Price:  ₦152,684.73
You save:  ₦45,810
₨19,719.68
List Price:  ₨28,172.18
You save:  ₨8,452.50
฿2,263.14
List Price:  ฿3,233.20
You save:  ฿970.05
₺2,881.58
List Price:  ₺4,116.73
You save:  ₺1,235.14
B$382.20
List Price:  B$546.03
You save:  B$163.82
R1,245.57
List Price:  R1,779.46
You save:  R533.89
Лв117.43
List Price:  Лв167.77
You save:  Лв50.33
₩97,558.65
List Price:  ₩139,375.48
You save:  ₩41,816.82
₪235.48
List Price:  ₪336.41
You save:  ₪100.93
₱3,998.15
List Price:  ₱5,711.89
You save:  ₱1,713.73
¥10,381.02
List Price:  ¥14,830.67
You save:  ¥4,449.64
MX$1,313.31
List Price:  MX$1,876.23
You save:  MX$562.92
QR254.80
List Price:  QR364.02
You save:  QR109.21
P939.68
List Price:  P1,342.46
You save:  P402.78
KSh9,042.70
List Price:  KSh12,918.70
You save:  KSh3,876
E£3,397.79
List Price:  E£4,854.20
You save:  E£1,456.40
ብር10,004.36
List Price:  ብር14,292.55
You save:  ብር4,288.19
Kz63,827.73
List Price:  Kz91,186.39
You save:  Kz27,358.65
CLP$67,779.01
List Price:  CLP$96,831.31
You save:  CLP$29,052.30
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,407.65
List Price:  RD$6,296.92
You save:  RD$1,889.26
DA9,093.02
List Price:  DA12,990.59
You save:  DA3,897.56
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q536.13
List Price:  Q765.93
You save:  Q229.80
GY$14,622.96
List Price:  GY$20,890.84
You save:  GY$6,267.87
ISK kr8,633.26
List Price:  ISK kr12,333.76
You save:  ISK kr3,700.50
DH635.79
List Price:  DH908.31
You save:  DH272.52
L1,169.35
List Price:  L1,670.58
You save:  L501.22
ден3,693.98
List Price:  ден5,277.34
You save:  ден1,583.36
MOP$561.67
List Price:  MOP$802.43
You save:  MOP$240.75
N$1,232.98
List Price:  N$1,761.48
You save:  N$528.49
C$2,572.20
List Price:  C$3,674.73
You save:  C$1,102.52
रु9,857.19
List Price:  रु14,082.30
You save:  रु4,225.11
S/247.23
List Price:  S/353.21
You save:  S/105.97
K291.61
List Price:  K416.61
You save:  K124.99
SAR262.60
List Price:  SAR375.17
You save:  SAR112.56
ZK1,662.23
List Price:  ZK2,374.72
You save:  ZK712.48
L305.21
List Price:  L436.04
You save:  L130.82
Kč1,469.69
List Price:  Kč2,099.65
You save:  Kč629.95
Ft23,673.83
List Price:  Ft33,821.21
You save:  Ft10,147.37
SEK kr662.12
List Price:  SEK kr945.93
You save:  SEK kr283.80
ARS$95,260.79
List Price:  ARS$136,092.68
You save:  ARS$40,831.89
Bs482.98
List Price:  Bs690.01
You save:  Bs207.02
COP$280,278
List Price:  COP$400,414.31
You save:  COP$120,136.30
₡35,335.67
List Price:  ₡50,481.69
You save:  ₡15,146.02
L1,832.33
List Price:  L2,617.73
You save:  L785.40
₲504,830.94
List Price:  ₲721,217.97
You save:  ₲216,387.03
$U2,804.51
List Price:  $U4,006.62
You save:  $U1,202.10
zł255.72
List Price:  zł365.33
You save:  zł109.61
Already have an account? Log In

Transcript

In this lecture, we will look into lifecycle hooks. So what are lifecycle hooks? lifecycle hooks are a very interesting concept. With the help of lifecycle hooks, we can understand when the component is being created when it is being destroyed when it is being updated, and when it is being mounted and not bore. So let's pull in the lifecycle hook flowchart. And we can look into that.

So this is the lifecycle hook flowchart. So if you can see the first thing, what we're doing is we are creating a new view instance or upon creating the new view instance it sets up all the events and lifecycle methods, so initializing events and life cycles, then what happens is, it fires off a lifecycle hook method, these before creating created, so and what are the hooks that we have outside of flowchart we can use these to run some code or we can use this to perform an action between these lifecycle events. So the first one is before create. So we can use before create before the view component has reactivity induced into it, as you can see after because we have the initiating of injections and reactivity, right So, after that the component is being created. So, so these lifecycle hooks are very, very important. I mean, these lifecycle hooks can be very useful.

So for example, before create and create either of these can be used to perform a network request. So while the component is being mounted and loaded, we can perform a network request and we can get the data. And once the component is created, basically checks for an element option. So, if you remember from the first lecture, when we integrated view into a existing website, we have an element option. So, if it has an element component will check for template option. But if it doesn't have an element component will go for mounting.

So, it'll check for the template after checking for the template. So if it has a template, it will compile that template into a render function. But if it doesn't have a template, it will compile the elements outer HTML template. So then we can go on to before mount and mounted. So these two are related to mounting the element onto the DOM, or getting the component onto the DOM. And after mounted, we have a couple of interesting lifecycle hooks such as before update an updated so these two lifecycle hooks can be used when the virtual DOM is being updated, or before the DOM is being updated or post, the DOM has been updated.

So we can perform some sort of action or either of these lifecycle hooks. So we can also monitor when the component is being destroyed. So before destroy and destroyed are the lifecycle hooks used for that. So in before destroy, we could perform some sort of saving the actions or if you can perform some sort of navigation actions, etc, and destroyed post destroyed, you could send out an event, or you could emit out an event, anything you might want to do when your component gets destroyed. Right. So these are the lifecycle hooks.

So let's try it out in our actual component so we can understand how it actually works. So what we'll do is we'll start off with the app component. So inside the app component, we'll have some data. This is the app component, right? So if I save it, and we can use the lifecycle hooks inside the script tag, similar to any of the properties, so we'll start the lifecycle hooks from the beginning. We'll create the before creating created methods before create so for every library Take a look let's create an alert message so we can understand when the lifecycle hook is being called.

After before create we get the created. Once created is done, we get the before mount and mounted. So before mountain mounted are used to mount the component onto the dome. So instead mounted we can say alert, mounted, and hopefully after mounted, so at this point, we can see the h1 tag whatever is here, this is the app component. It won't be it won't be visible to us until the mounted function is executed. So once the component is mounted onto the DOM, then we can see the component or I'm sorry, we can see the element.

So then we have two more lifecycle hooks before destroy and destroyed. I guess we're looking at before destroy and destroyed, we might have to create another component. So for now we'll just go with the two more lifecycle hooks before upgrading updated Alright, if I save it, I have it running in the local server. And if I say local server we get the first alert. So initially it is a stage of before create. So once the before create is done so so now it has created a new view instance.

And it has initialized the events in lifecycle hooks. So once the lifecycle events have been initialized, we can have access to the lifecycle hooks. So hence the befo create method. And if I say okay, I'm sorry. So if I refresh it, so if I say okay, I get the created and at this point of time, it will check for the element option and to refresh again. So after created if I say okay, I'm going to get the before bound.

So now it's going to mount the component onto the dome. So if I say okay, now mounted is done. So after clicking OK, we should be able to see the h1 tag. So okay, and that h1 tag. So that's what we wrote, right? So now before we look into before destroyed and destroyed, let's look into before upgrading updated right.

So for that to work let's go to another window. We'll throw that in there. Yeah, for that to work we need to create a data component i mean data property inside the data property, let's have a variable, let's say name, just a common example. And let's have an input field placeholder enter name and let's bind it to name using the model. So now if I save it, so what and the component has to refresh right? So if I I'll have to go through every single thing again.

And before mount and before destroy, yeah, so since the component is being rebuilt again, we are getting there before destroy and destroyed lifecycle hooks here. So mounted and this is the component and here's the name. So every single time I enter something onto this, so every single time I enter something onto this, the DOM changes or the data property changes, and the DOM has to update, right. So every single time the DOM has to update, it will fire of the lifecycle hooks before update and update, let's say entered, we and navigate the before update. So before upgrade, we could perform some sort of action. Before the DOM is getting updated.

We can perform any action if we want to. And once the update is done, we'll get another lifecycle hook that says updated. So inside this updated method, we can either intimate the user that some data has been changed or anything has been done. If I say, okay, that's you. So that will happen for every single instance, because I have an alert for every before updating updated, right. So now let's look into before destroy and destroyed.

To understand this much better, let's pull in the login component instead Login we'll let's just throw in the before destroy and destroy it right so let's also comment these out, save them Alright, so the reason why I have login form here is let's also come in these out. Since will not be needing these. Let's also create a button that says destroy login component and inside the data property let's have a boolean value. And for login let's have a Vf. Show login. For the button, let's have an event.

Click event and we can just alter the value of show logon. Since it's a single line, we can just start it So if everything went right, if this component is destroyed, it should fire off to events that says before before destroyed and destroyed. So let's try that. So if I press destroy logging component i get before destroyed. And if I say okay, I get a destroyed, then the login component is no longer visible. So if I click again it appears and I can destroy it again.

Right so that's good. So these are lifecycle hooks and in the next lecture we will look into routing, what is view router and how why routing is required. And that's it for this one. I'll see you the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.