Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,860 --> 00:00:07,400
In this lecture, we're going to learn how to use template refs with the composition API sample at RAF's,
2
00:00:07,400 --> 00:00:11,300
as we've learned, are a way to interact with the elements in a component.
3
00:00:11,720 --> 00:00:15,790
They expose the native Browsr API for interacting with elements.
4
00:00:16,100 --> 00:00:20,060
It's a feature available in case any other solution is not viable.
5
00:00:20,750 --> 00:00:24,960
Working with template references is a slightly different process than before.
6
00:00:25,370 --> 00:00:28,020
Let's look at how it's done in the template.
7
00:00:28,040 --> 00:00:30,800
Let's add a button at the bottom of the template.
8
00:00:33,440 --> 00:00:39,410
We're going to add an event listener to this button, however, for this demonstration, we're not going
9
00:00:39,410 --> 00:00:43,790
to use views, binding syntax for listening events on elements.
10
00:00:44,150 --> 00:00:45,950
We'll use template references.
11
00:00:46,370 --> 00:00:50,650
The syntax for using template references in the template hasn't changed.
12
00:00:50,960 --> 00:00:53,990
We can add the ref attribute to the button element.
13
00:00:54,410 --> 00:00:56,480
The name for the reference will be button.
14
00:00:59,050 --> 00:01:05,440
The next step is to add an event listener, however, there is a problem in our setup function, we
15
00:01:05,440 --> 00:01:07,870
don't have access to the this keyword.
16
00:01:08,200 --> 00:01:12,700
It's because these setup function is called before the component is instantiated.
17
00:01:13,090 --> 00:01:17,500
Template references are accessible via the dollar sign ref's variable.
18
00:01:18,950 --> 00:01:24,740
If we want to access template references, we need to create a variable that will store the reference.
19
00:01:25,040 --> 00:01:27,710
This might be confusing, but just follow along.
20
00:01:27,870 --> 00:01:29,930
I'll explain what's going on in a moment.
21
00:01:30,440 --> 00:01:34,960
At the top of the setup, function will create a variable called button.
22
00:01:35,360 --> 00:01:38,930
Its value will be the ref function with a value of null.
23
00:01:41,500 --> 00:01:47,830
The ref function isn't limited to storying primitive values, it's able to store template references
24
00:01:47,830 --> 00:01:54,460
to if we plan on using a reactive reference to store a template reference, its initial value must be
25
00:01:54,460 --> 00:01:54,850
null.
26
00:01:57,450 --> 00:02:02,970
Nicks, in the return statement, we can add this reference to the list of values to return.
27
00:02:05,580 --> 00:02:12,510
When the set up function is called view will create the reactive reference, it won't be bound to anything
28
00:02:12,510 --> 00:02:16,440
until the component has been mounted during the mounting phase.
29
00:02:16,770 --> 00:02:20,010
You will notice we have an element with the eref attribute.
30
00:02:20,430 --> 00:02:26,340
The value for the ref attribute must correspond to the name of the reactive reference we have in our
31
00:02:26,340 --> 00:02:27,240
setup function.
32
00:02:27,690 --> 00:02:33,420
We will assign the element to the reference, will be able to interact with the element in our setup
33
00:02:33,430 --> 00:02:35,950
function after the component has been mounted.
34
00:02:36,330 --> 00:02:38,880
We can proceed to add an event listener.
35
00:02:40,130 --> 00:02:46,120
In our setup function, it's important we add the event listener after the component has been mounted,
36
00:02:46,490 --> 00:02:51,770
if we were to attempt to add the event, listener will receive an error because the button variable
37
00:02:51,770 --> 00:02:52,580
will be null.
38
00:02:53,240 --> 00:02:56,300
Luckily, we talked about lifecycle functions already.
39
00:02:56,630 --> 00:03:00,920
We already have the on mounted life cycle function in our function.
40
00:03:01,340 --> 00:03:08,000
We can update the callback function by calling the ADD event listener function on the button value property.
41
00:03:10,580 --> 00:03:16,490
Keep in mind, we're still using a reactive reference if we want to access the value, which in this
42
00:03:16,490 --> 00:03:20,610
case is the element, we'll need to access it via the value property.
43
00:03:21,020 --> 00:03:23,030
That's where a view will store the element.
44
00:03:23,810 --> 00:03:27,190
The name of the event we'll listen to will be the click event.
45
00:03:29,740 --> 00:03:35,320
Next in the callback function will log a message to let us know if the button was clicked.
46
00:03:37,940 --> 00:03:40,310
Let's test the button in the browser.
47
00:03:42,730 --> 00:03:49,180
If I press the button, the message gets logged from the event listeners callback function great, we've
48
00:03:49,180 --> 00:03:53,100
learned how to use template references with the composition API.
49
00:03:53,530 --> 00:03:58,390
It might seem a little confusing because there are two types of references we're working with.
50
00:04:00,960 --> 00:04:07,860
To add clarity, let's talk about the differences in the template, we add the ref attribute to create
51
00:04:07,860 --> 00:04:14,580
a template reference, a template reference is an object that allows us to interact with the HTML element
52
00:04:14,580 --> 00:04:17,329
and the document in our setup function.
53
00:04:17,339 --> 00:04:21,810
When we called the ref function, we are creating a reactive reference.
54
00:04:22,140 --> 00:04:26,280
A reactive reference is an object that's being observed for changes.
55
00:04:26,910 --> 00:04:33,390
We mainly use reactive references for storing primitive values, but it can be used to store a template
56
00:04:33,390 --> 00:04:35,520
reference that doesn't for now.
57
00:04:35,640 --> 00:04:37,140
I'll see you in the next one.
6112
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.