Props and Custom Events

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
€65.59
List Price:  €93.70
You save:  €28.11
£56.03
List Price:  £80.05
You save:  £24.01
CA$96.39
List Price:  CA$137.71
You save:  CA$41.31
A$107.98
List Price:  A$154.26
You save:  A$46.28
S$95.50
List Price:  S$136.44
You save:  S$40.93
HK$547.57
List Price:  HK$782.28
You save:  HK$234.70
CHF 64.43
List Price:  CHF 92.05
You save:  CHF 27.61
NOK kr776.82
List Price:  NOK kr1,109.79
You save:  NOK kr332.97
DKK kr489.21
List Price:  DKK kr698.91
You save:  DKK kr209.69
NZ$118.81
List Price:  NZ$169.74
You save:  NZ$50.92
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,729.18
List Price:  ৳11,042.16
You save:  ৳3,312.97
₹5,842.93
List Price:  ₹8,347.41
You save:  ₹2,504.47
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,745.84
List Price:  ₦132,499.74
You save:  ₦39,753.90
₨19,599.98
List Price:  ₨28,001.17
You save:  ₨8,401.19
฿2,600.12
List Price:  ฿3,714.62
You save:  ฿1,114.50
₺2,271.31
List Price:  ₺3,244.87
You save:  ₺973.56
B$363.64
List Price:  B$519.51
You save:  B$155.87
R1,308.28
List Price:  R1,869.05
You save:  R560.77
Лв128.30
List Price:  Лв183.29
You save:  Лв54.99
₩96,996.60
List Price:  ₩138,572.52
You save:  ₩41,575.91
₪261.41
List Price:  ₪373.46
You save:  ₪112.04
₱4,045.59
List Price:  ₱5,779.67
You save:  ₱1,734.07
¥11,052.75
List Price:  ¥15,790.32
You save:  ¥4,737.57
MX$1,197.81
List Price:  MX$1,711.23
You save:  MX$513.42
QR257.31
List Price:  QR367.61
You save:  QR110.29
P1,000.11
List Price:  P1,428.79
You save:  P428.68
KSh9,335.98
List Price:  KSh13,337.69
You save:  KSh4,001.70
E£3,349.95
List Price:  E£4,785.86
You save:  E£1,435.90
ብር4,013.63
List Price:  ብር5,734.01
You save:  ብር1,720.37
Kz58,409.45
List Price:  Kz83,445.65
You save:  Kz25,036.20
CLP$67,298.07
List Price:  CLP$96,144.23
You save:  CLP$28,846.15
CN¥506.79
List Price:  CN¥724.02
You save:  CN¥217.23
RD$4,121.81
List Price:  RD$5,888.55
You save:  RD$1,766.74
DA9,423.87
List Price:  DA13,463.25
You save:  DA4,039.38
FJ$159.96
List Price:  FJ$228.52
You save:  FJ$68.56
Q547.38
List Price:  Q782
You save:  Q234.62
GY$14,742.16
List Price:  GY$21,061.13
You save:  GY$6,318.97
ISK kr9,832.89
List Price:  ISK kr14,047.59
You save:  ISK kr4,214.70
DH712.24
List Price:  DH1,017.53
You save:  DH305.29
L1,243.01
List Price:  L1,775.81
You save:  L532.79
ден4,046.68
List Price:  ден5,781.23
You save:  ден1,734.54
MOP$564.08
List Price:  MOP$805.87
You save:  MOP$241.78
N$1,315.96
List Price:  N$1,880.02
You save:  N$564.06
C$2,592.36
List Price:  C$3,703.54
You save:  C$1,111.17
रु9,349.94
List Price:  रु13,357.63
You save:  रु4,007.69
S/263.70
List Price:  S/376.73
You save:  S/113.03
K271.96
List Price:  K388.53
You save:  K116.57
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,886.12
List Price:  ZK2,694.57
You save:  ZK808.45
L326.39
List Price:  L466.29
You save:  L139.90
Kč1,650.20
List Price:  Kč2,357.53
You save:  Kč707.33
Ft25,629.21
List Price:  Ft36,614.73
You save:  Ft10,985.52
SEK kr770.71
List Price:  SEK kr1,101.06
You save:  SEK kr330.35
ARS$61,698.11
List Price:  ARS$88,143.94
You save:  ARS$26,445.82
Bs486.86
List Price:  Bs695.54
You save:  Bs208.68
COP$271,471.23
List Price:  COP$387,832.67
You save:  COP$116,361.43
₡35,819.54
List Price:  ₡51,172.97
You save:  ₡15,353.42
L1,740.31
List Price:  L2,486.27
You save:  L745.95
₲526,285.13
List Price:  ₲751,868.13
You save:  ₲225,582.99
$U2,699.91
List Price:  $U3,857.19
You save:  $U1,157.27
zł284.80
List Price:  zł406.88
You save:  zł122.07
Already have an account? Log In

Transcript

In this lecture, we will look into how to send data to components, how to send data to components and how to get back data from components. So, this data is often referred to as props. So, now we will look into how to pass data from parent component A child component as well as getting bad data from child component to parent component. So, we already have a data component we created in the last lecture and inside this component there is nothing much more data I showed that I am a data component. So, we will try to send this data this string from the parent component for example, we are going abroad right. So, instead abroad, we will import data component.

We will register the component under the components property. Now, we can use the component and we can say data this so if I refresh it, okay, we get nothing because there's nothing in here. So we'll just have a heading that says need to have one route. So we'll have data as a root div and inside this will have an h tag h1 title says data component. Alright, so we can see data component. And so now what we want to do is we want to send data into this component, right?

So, for example, let's say I want to have a history tag. I want to have a history tag. Here, I want to show display a message. Right. So this message, I want to send it from the parent component, which is a broad view. To send that message we will use something called as props.

So the data component is expecting a prop within a message. So we can declare it inside a script tag, we can say props. Inside props, we can say the name of the property and expecting in our case it is message. And this is optional, but it's better to have it. It's a good practice to have this. So generally, you would Just limited message and that would be it.

But in terms of good practice, it's usually a good thing to do this here we can declare the type, it is a string, and is it required and it is true. So these are kind of like additional properties to that one little prop I mentioned required is true right for this problem. So if I save it and insert a parent component, we are not sending any data, we are not sending the message prop which it expects, right? So if I go to our website, and if I open the console log, we will get a view warning, missing required prop message because we declared the type as true. And it's a type string. All right.

So now to send this message, what we can do is inside the data component, we can say, colon, message. And inside this message, I can pass a data property. So let's create a data property that says message. And let's say hello from data component. And here we are going to say, message. So if I save it, we can see that.

So if I refresh it, the error sign goes away because we are sending in the data. So we can also send him multiple props. For example, if it's also requiring each Prop, you can see each type should be number 10. Safe, quiet. False. So since age is not mandatory, we can just say required is false.

And if I save it, we don't get an error regarding age because required is false. And set up inside our parent component. We can have another property called his age. And in today's I can send them the value I'm sorry, so guess to display the age here. Age and age. So ages 10 credits, so that is sending data from parent component to child component.

But how do we send data from child component Back to parent component. So we will use something called as Emmet. Okay, so we'll do one thing. We will remove data component from our abroad view. And we'll import log in component. And we'll register it as login and send the components property.

And we can use it, login. I save it. I'm getting an error count resolved login components, right? Its components. So if I save it have the login form here, inside the login form. I can have a button and say submit and give it a type of Submit.

And we can have an eventlistener submit and we can prevent the default behavior by saying Prevent, and we can say so Bitcoin. So instead of script tag, we can have a property inside the methods property, we can declare our method submit form. For now we'll just a console log console dot log form has been submitted, right. So if I save it and press enter, I could become sold message form has been submitted. So now what I want to do is instead of a broad view, I want to create a paragraph or I want to rather have a heading tag say message and inside this message, I will remove this message and I basically want to know if the form has been submitted or not. So firstly, form has not been submitted.

Right. So we can even have an emoji here. So if I see form has not been submitted, so what we want is, so when I go to log in form, and I hit the submit button, I want the message of here in the abroad view to change. Alright, so what we can do is, instead of console logging it, we can say this dot dollar emit on Submit. Alright, so basically what's happening is we are kind of like sending out a trigger to App dot view that the form has been submitted right. so here we can receive the trigger by going add on submit submitted.

So upon receiving this trigger from the login component, we can trigger our own method inside the app component. So on submit am creating the method form submit So if I save it, and if I go to methods, and if I say form submitted, and inside this, I tried to change the message to this dot message equal to form submitted, right? Let's try. If I go into our website and if I hit submit, the message gets changed. So just to go over what happened here is inside the login component, when the form is being submitted, we are emitting an event and this event name is on Submit. It's a custom event that we designed, we are receiving is on submit event inside our app you log in is defined and an on submit on this event occurring, we are trying to execute this form submitted method and inside this form submitted, we are changing the message to form submitted.

So if I refresh it, we have form has not been submitted, click form submitted. Right. So this is how to send data from parent component to child component using props, and getting back data from child components to parent components by using events. Right. In the next lecture, we will look into lifecycle hooks of a component. That should be interesting.

I'll see you in 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.