Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,730 --> 00:00:06,070
In this lecture, we're going to look at another function for creative reactive references.
2
00:00:06,100 --> 00:00:10,620
View provides us with two functions for creating reactive references.
3
00:00:10,630 --> 00:00:13,230
The first function is the ref function.
4
00:00:13,240 --> 00:00:15,940
The second function is called reactive.
5
00:00:15,940 --> 00:00:18,790
We'll learn about the reactive function next.
6
00:00:18,790 --> 00:00:22,660
Afterward, we'll discuss the differences between the two functions.
7
00:00:22,900 --> 00:00:24,340
In the setup function.
8
00:00:24,340 --> 00:00:27,250
We're going to create a variable called user.
9
00:00:27,250 --> 00:00:29,110
It'll be set to an object.
10
00:00:31,430 --> 00:00:36,690
One thing I want you to notice is that we're defining the variable below the increment function.
11
00:00:36,710 --> 00:00:41,300
We can place the variable definition anywhere we want in the setup function.
12
00:00:41,330 --> 00:00:45,890
This flexibility is one of the main benefits of the composition API.
13
00:00:46,010 --> 00:00:48,620
We can arrange the code inside the function.
14
00:00:48,620 --> 00:00:53,990
By logic, the number variable and increment function are grouped together.
15
00:00:54,020 --> 00:00:56,660
We don't have to scroll around to work with it.
16
00:00:57,020 --> 00:01:03,260
Moving on will create two properties in the user object called name and age.
17
00:01:03,290 --> 00:01:06,050
Assign the properties any values you'd like.
18
00:01:08,390 --> 00:01:10,340
Just like the number variable.
19
00:01:10,340 --> 00:01:12,090
This will not be reactive.
20
00:01:12,110 --> 00:01:18,620
If we were to add the variable to the list of values to return, it'd be available in the template during
21
00:01:18,620 --> 00:01:19,890
the mounting phase.
22
00:01:19,910 --> 00:01:22,520
However, it will not be reactive.
23
00:01:22,520 --> 00:01:27,500
We can make it reactive with the reactive function at the top of the script.
24
00:01:27,500 --> 00:01:33,200
BLOCK will add on to the list of functions to import by adding the reactive function.
25
00:01:35,590 --> 00:01:38,500
This idea of importing function is a common thing.
26
00:01:38,500 --> 00:01:42,700
We'll have to perform whenever we want to add functionality to our component.
27
00:01:42,850 --> 00:01:47,290
The composition API requires we import the functions we want.
28
00:01:47,320 --> 00:01:50,950
They do not automatically get injected into the component.
29
00:01:51,280 --> 00:01:55,510
Will wrap the user object with the reactive function.
30
00:01:57,740 --> 00:02:00,830
Next, we'll return the user object.
31
00:02:03,260 --> 00:02:07,610
We'll want to test its reactivity below the variable definition.
32
00:02:07,610 --> 00:02:11,210
We'll create a timeout with the set timeout function.
33
00:02:11,240 --> 00:02:13,970
It'll run after 3 seconds has passed.
34
00:02:16,240 --> 00:02:22,480
Inside the callback function, we'll update the user name property to a different value.
35
00:02:24,870 --> 00:02:29,080
This is one difference between the reactive and ref functions.
36
00:02:29,100 --> 00:02:33,980
The value returned by the reactive function is an object with reactivity.
37
00:02:33,990 --> 00:02:38,220
The properties don't need to be accessed via a value property.
38
00:02:38,220 --> 00:02:40,860
You can access them like you regularly would.
39
00:02:41,220 --> 00:02:44,400
The last thing we'll do is use the object in the template.
40
00:02:44,400 --> 00:02:46,260
Scroll to the template block.
41
00:02:46,260 --> 00:02:48,930
We'll add a paragraph tag below the button.
42
00:02:51,220 --> 00:02:54,270
Well, insert an expression inside the tag.
43
00:02:54,280 --> 00:02:57,700
The expression will be set to username.
44
00:03:00,040 --> 00:03:01,810
Let's refresh the page.
45
00:03:04,100 --> 00:03:09,290
After 3 seconds the name will change because we updated it in a timeout function.
46
00:03:09,320 --> 00:03:14,030
The reactive function is another way to make a variable have reactivity.
47
00:03:14,060 --> 00:03:17,300
It doesn't seem that much different than the ref function.
48
00:03:17,300 --> 00:03:19,970
So what are the differences between the two?
49
00:03:21,630 --> 00:03:25,920
The biggest difference is the types of values you can use in either one.
50
00:03:25,950 --> 00:03:32,700
The ref function is meant for primitive values such as strings, numbers and booleans.
51
00:03:32,730 --> 00:03:37,830
If you want to create a reactive object, you'll need to use the reactive function.
52
00:03:38,130 --> 00:03:44,790
You can't pass a primitive value to the reactive function, otherwise you will receive errors for doing
53
00:03:44,790 --> 00:03:45,330
so.
54
00:03:45,360 --> 00:03:50,130
Both functions add reactivity to your values, but for different data types.
55
00:03:50,580 --> 00:03:55,080
The other difference between the two functions is how values are accessed.
56
00:03:55,110 --> 00:04:01,440
The ref function returns an object where the original value is stored in a property called value.
57
00:04:01,710 --> 00:04:07,020
If you're using the reactive reference in a template, you don't have to access it through the value
58
00:04:07,020 --> 00:04:07,860
property.
59
00:04:07,890 --> 00:04:11,220
View will take care of unwrapping the object for you.
60
00:04:11,250 --> 00:04:14,790
The value can be accessed directly through the variable.
61
00:04:15,030 --> 00:04:18,170
The reactive function doesn't move your values.
62
00:04:18,180 --> 00:04:21,610
They will remain in the exact location you defined them.
63
00:04:21,630 --> 00:04:26,100
This applies to when you're using them in the setup function and in the template.
64
00:04:27,380 --> 00:04:29,480
This brings us to the next question.
65
00:04:29,480 --> 00:04:31,250
When should we use either one?
66
00:04:31,280 --> 00:04:37,040
Theoretically, you can use the reactive function once to store all the components data.
67
00:04:37,070 --> 00:04:40,400
However, this is not recommended for two reasons.
68
00:04:40,400 --> 00:04:45,410
Firstly, you miss out on the organisation the composition API offers.
69
00:04:45,410 --> 00:04:47,660
It defeats the purpose of using it.
70
00:04:47,690 --> 00:04:51,610
Secondly, there are limitations to the reactive function.
71
00:04:51,620 --> 00:04:56,300
We can't structure the object, nor can we use the spread operator.
72
00:04:56,330 --> 00:05:00,860
This limitation is something you may want to do to make your code more readable.
73
00:05:00,890 --> 00:05:02,540
Let me show you what I mean.
74
00:05:02,930 --> 00:05:08,330
In the return statement, we're going to add the spread operator to the user variable.
75
00:05:10,570 --> 00:05:15,090
We'll update the expression we used in the template for the user object.
76
00:05:15,100 --> 00:05:19,450
We'll update the expression from user name to name.
77
00:05:21,740 --> 00:05:24,170
Let's refresh the page on the browser.
78
00:05:26,470 --> 00:05:30,370
After 3 seconds have passed, the name will remain the same.
79
00:05:30,400 --> 00:05:35,290
This is because we lose reactivity if we were to attempt to spread the object.
80
00:05:35,320 --> 00:05:39,160
However, it is possible to have the best of both worlds.
81
00:05:39,190 --> 00:05:47,080
View introduces a function for converting a reactive object's properties into individual reactive references.
82
00:05:47,350 --> 00:05:49,680
Let's go back to the editor for a moment.
83
00:05:49,690 --> 00:05:56,260
We're going to update the import statement for the View package by adding a function called two reps.
84
00:05:58,520 --> 00:06:04,850
The two refs function converts an object's properties into separate reactive references.
85
00:06:04,850 --> 00:06:11,900
In the return statement, we can wrap the user variable with the two refs function before spreading
86
00:06:11,900 --> 00:06:12,230
it.
87
00:06:14,700 --> 00:06:19,470
This function will allow us to keep the object, but shorten the names in the template.
88
00:06:19,500 --> 00:06:24,030
Let's refresh the page to check if the name updates after 3 seconds.
89
00:06:26,320 --> 00:06:28,400
The name becomes reactive again.
90
00:06:28,420 --> 00:06:34,750
The two refs function assists you with spreading an object while also allowing you to keep the properties
91
00:06:34,750 --> 00:06:35,740
reactive.
92
00:06:35,770 --> 00:06:42,100
Despite its availability, I don't recommend using it for storing the entire components data in a single
93
00:06:42,100 --> 00:06:42,850
object.
94
00:06:42,850 --> 00:06:47,140
Data in objects should be grouped by how relative they are to one another.
95
00:06:47,440 --> 00:06:54,100
In the resource section of this lecture, I provide a link to the topic of refs versus reactive.
96
00:06:54,100 --> 00:07:00,160
I recommend reading this after you've become more familiar with the composition API, it gives a bit
97
00:07:00,160 --> 00:07:03,880
more information about the differences between the two functions.
9570
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.