Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,120 --> 00:00:05,280
In this lecture, we're going to start working with the state to accomplish our goal of toggling the
2
00:00:05,280 --> 00:00:09,540
modal we've already taken care of, breaking the template into components.
3
00:00:09,540 --> 00:00:12,810
This breakdown reduced clutter in our application.
4
00:00:13,080 --> 00:00:15,270
The next step is to toggle the modal.
5
00:00:15,270 --> 00:00:17,580
Here's how we're going to approach this.
6
00:00:17,580 --> 00:00:21,330
We'll need to store value that will either be true or false.
7
00:00:21,330 --> 00:00:22,530
The modal is visible.
8
00:00:22,530 --> 00:00:24,690
Appearance will depend on that value.
9
00:00:24,720 --> 00:00:27,150
This means that we'll have to keep track of it.
10
00:00:27,150 --> 00:00:29,400
We'll need a way to toggle the value.
11
00:00:29,430 --> 00:00:35,370
We can store the value in a component as a data property, but I believe storing it in the state is
12
00:00:35,370 --> 00:00:36,450
a better approach.
13
00:00:36,480 --> 00:00:39,720
It'll allow for other components to toggle the value.
14
00:00:40,110 --> 00:00:42,960
Peneha was created for situations like this.
15
00:00:42,960 --> 00:00:46,350
It can contain data that will be available to all components.
16
00:00:46,350 --> 00:00:50,040
Any updates to the data are reflected in all components.
17
00:00:50,040 --> 00:00:55,320
This centralized state makes it ideal for holding the flag value of the modal.
18
00:00:55,320 --> 00:00:59,430
First, we'll create the value in the stores directory.
19
00:00:59,430 --> 00:01:02,400
Create a file called Modal JS.
20
00:01:04,470 --> 00:01:05,910
At the top of the file.
21
00:01:05,910 --> 00:01:09,960
Import the defined store function from the package.
22
00:01:12,100 --> 00:01:15,440
The defined store function will help us create a new store.
23
00:01:15,460 --> 00:01:19,960
We can think of the store as an object that will track the data stored inside it.
24
00:01:19,990 --> 00:01:26,590
The store must be exported from the file, otherwise our components will not have access to the data.
25
00:01:26,680 --> 00:01:31,600
Export the defined store function from the default namespace.
26
00:01:33,730 --> 00:01:36,120
This function has to arguments.
27
00:01:36,130 --> 00:01:38,920
The first argument is an ID for the store.
28
00:01:38,950 --> 00:01:42,040
It's completely normal to have multiple stores.
29
00:01:42,100 --> 00:01:46,570
Technically, you can create a single store for an entire application.
30
00:01:46,570 --> 00:01:53,950
However, it's considered good practice to split your state into multiple stores for easier manageability.
31
00:01:54,010 --> 00:01:57,100
The ID will allow us to identify the store.
32
00:01:57,130 --> 00:01:59,410
Let's set the ID to Modal.
33
00:02:01,510 --> 00:02:03,640
Next pass in an object.
34
00:02:03,640 --> 00:02:08,610
In this object we can add various properties for interacting with our state.
35
00:02:08,620 --> 00:02:12,700
Before we define functions, let's add the state property.
36
00:02:12,700 --> 00:02:17,620
The value for this property will be an arrow function that returns an object.
37
00:02:19,750 --> 00:02:26,410
Inside the state object create a property called is open with an initial value of false.
38
00:02:28,540 --> 00:02:31,300
The state object is where we can add our data.
39
00:02:31,330 --> 00:02:35,960
Any data that you want globally available to other components should be defined here.
40
00:02:35,980 --> 00:02:37,260
The data is ready.
41
00:02:37,270 --> 00:02:40,150
Let's learn how to access it from a component.
42
00:02:40,480 --> 00:02:43,540
Open the header component file in your editor.
43
00:02:45,810 --> 00:02:49,280
Inside the header component, we have a navigation menu.
44
00:02:49,290 --> 00:02:53,490
It's an unordered list with tailwind classes above the menu.
45
00:02:53,490 --> 00:02:56,610
There's a comment that says Primary navigation.
46
00:02:56,910 --> 00:03:00,960
We're going to focus our attention on the link that says log and register.
47
00:03:03,030 --> 00:03:06,450
If the user clicks, this link will want to display the modal.
48
00:03:06,480 --> 00:03:12,000
The model's appearance will depend on the value of the is open stage property.
49
00:03:12,180 --> 00:03:14,730
If it's true, the modal will be visible.
50
00:03:14,760 --> 00:03:16,710
If it's false, it'll be hidden.
51
00:03:16,950 --> 00:03:20,130
Essentially, clicking this link should toggle the value.
52
00:03:20,160 --> 00:03:23,180
We'll attach an event listener to the anchor element.
53
00:03:23,190 --> 00:03:27,090
We're going to listen for the Click Event with the Prevent Modifier.
54
00:03:29,240 --> 00:03:32,690
We're going to set the value to toggle off modal.
55
00:03:34,740 --> 00:03:36,930
We won't pass anything to the function.
56
00:03:36,960 --> 00:03:40,300
The function doesn't exist, so we'll define it next.
57
00:03:40,320 --> 00:03:46,770
Below in the script block set the name to header and create the methods object.
58
00:03:46,800 --> 00:03:49,170
If the script block does not exist.
59
00:03:49,170 --> 00:03:50,130
Added in.
60
00:03:52,440 --> 00:03:57,270
Inside the methods object will define the toggle off modal function.
61
00:03:59,540 --> 00:04:04,470
This function will be responsible for toggling the boolean value we created in the state.
62
00:04:04,490 --> 00:04:07,280
The question is how do we access the state?
63
00:04:07,490 --> 00:04:12,860
Penina has a function for merging the store into our components at the top of the scripts.
64
00:04:12,860 --> 00:04:17,930
BLOCK import a function called map stores from the Peneha package.
65
00:04:20,079 --> 00:04:25,600
This function will merge the store into the component, thus giving us access to the state.
66
00:04:25,630 --> 00:04:28,900
Before running this function, we're going to need the store.
67
00:04:28,930 --> 00:04:31,570
Import the store with the following name.
68
00:04:31,570 --> 00:04:33,220
Use modal store.
69
00:04:35,520 --> 00:04:40,230
It's common practice to follow a specific naming convention for your stores.
70
00:04:40,260 --> 00:04:45,840
The name must always be prefixed with the word use and affixed with the word store.
71
00:04:45,870 --> 00:04:51,160
This naming convention helps developers understand that we're using the store in the component.
72
00:04:51,180 --> 00:04:53,170
It's optional, but recommended.
73
00:04:53,190 --> 00:04:58,830
We're going to follow this practice for the rest of the course and the components options.
74
00:04:58,830 --> 00:05:01,980
Add the computed object in this object.
75
00:05:01,980 --> 00:05:05,880
Call the map stores function with the spread operator.
76
00:05:08,040 --> 00:05:09,990
This function accepts the store.
77
00:05:10,020 --> 00:05:14,070
Let's pass in the use modal store object.
78
00:05:16,130 --> 00:05:23,780
Back in the toggle off modal function the stores now accessible through the this dot modal store object.
79
00:05:25,890 --> 00:05:30,180
The map store's function will create an object with the name of the ID.
80
00:05:30,210 --> 00:05:34,230
In our case, the ID of the store was set to modal.
81
00:05:34,260 --> 00:05:38,580
You can refer to the modal JS file for the name of the ID.
82
00:05:38,610 --> 00:05:41,290
The word store gets appended to the name.
83
00:05:41,310 --> 00:05:47,120
Therefore, if we had a store called User, the object would be called user store.
84
00:05:47,130 --> 00:05:51,390
Inside the function, we'll want to set the current value to its opposite.
85
00:05:56,370 --> 00:06:02,640
This is a simple trick to toggle boolean values to verify if the value gets toggled, we're going to
86
00:06:02,640 --> 00:06:04,380
log the value afterward.
87
00:06:06,580 --> 00:06:08,710
Let's check if everything is working.
88
00:06:08,740 --> 00:06:13,930
Opening the browser will open the developer tools and switch to the console panel.
89
00:06:16,120 --> 00:06:19,100
Then I'll click the log in register link.
90
00:06:19,120 --> 00:06:23,020
After doing so numerous times, we can see the value is changing.
91
00:06:23,020 --> 00:06:25,270
We have a way to update the state.
92
00:06:25,270 --> 00:06:28,840
We are allowed to access the state through the store object.
93
00:06:28,840 --> 00:06:31,510
We can even change the value if we'd like.
94
00:06:31,570 --> 00:06:32,260
Great.
95
00:06:32,260 --> 00:06:37,060
We've successfully created our first state property in the next lecture.
96
00:06:37,060 --> 00:06:39,910
Let's begin applying this state to the modal.
9101
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.