Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,469 --> 00:00:09,410
Saudi context API is not ideal, but there is a different way you could manage this and for this all
2
00:00:09,520 --> 00:00:14,440
to create a new folder and a name it bookstore, you could name it just story, but I already have a
3
00:00:14,440 --> 00:00:20,320
story folder for my redox stuff, and I don't want to delete that because now I will build my own global
4
00:00:20,320 --> 00:00:25,990
state management storage solution with just JavaScript, react and react hooks and nothing else, and
5
00:00:25,990 --> 00:00:29,830
also not the context API in that Hooke's store here.
6
00:00:30,040 --> 00:00:37,150
I'll create a new store, just fall and in that fall on, I'll show you a solution of managing state
7
00:00:37,150 --> 00:00:39,970
globally with just will react in JavaScript.
8
00:00:40,480 --> 00:00:48,580
So let's say we're using Deadfall to manage our global state and I'm deliberately using a very generic
9
00:00:48,580 --> 00:00:54,160
name here because we'll be able to use this store for all kind of state, not just for our products.
10
00:00:54,910 --> 00:00:56,520
Global state could be an object.
11
00:00:57,220 --> 00:01:03,220
Now, we also want to have a couple of places in the app where we can listen to changes in that state.
12
00:01:03,700 --> 00:01:07,280
So all that a variable listeners hear, that's an array.
13
00:01:07,840 --> 00:01:14,620
Please note that both these things are normal variables in this file block scoped.
14
00:01:14,620 --> 00:01:18,490
So not globally available variables, but only available in that file.
15
00:01:18,490 --> 00:01:21,880
But they're not tied to any class or anything like that.
16
00:01:21,970 --> 00:01:23,190
They're just in that file.
17
00:01:24,400 --> 00:01:25,720
They're also not exported.
18
00:01:25,900 --> 00:01:31,300
So we got listeners, we got the state will also need a couple of actions which can be dispatched and
19
00:01:31,300 --> 00:01:33,310
that's, let's say, is an empty object for now.
20
00:01:34,910 --> 00:01:40,400
OK, so now we got these three things, now let's create a custom hook and definitely have a look at
21
00:01:40,400 --> 00:01:46,550
the React Hooks module first so that you have an idea of how hooks work and how you would build custom
22
00:01:46,550 --> 00:01:46,880
hooks.
23
00:01:47,750 --> 00:01:52,310
So here I'll create a custom hook and you name it, use store and just like.
24
00:01:52,310 --> 00:01:53,000
All right, Hooks.
25
00:01:53,000 --> 00:01:55,730
It's a function just to function like this.
26
00:01:57,050 --> 00:02:03,070
Now, in this hook here in this custom hook, I can, of course, use react hooks.
27
00:02:03,200 --> 00:02:10,520
So now let's import such a right hook and there let's import the use state hook.
28
00:02:10,910 --> 00:02:19,100
Because my goal here now is that I have some mechanism that could lead to other components to render
29
00:02:19,100 --> 00:02:19,610
and use.
30
00:02:19,610 --> 00:02:21,140
State has such a mechanism.
31
00:02:21,470 --> 00:02:23,360
You state allows us to manage a state.
32
00:02:23,360 --> 00:02:28,600
And whenever we update that state, any component that uses you as state will re render.
33
00:02:28,790 --> 00:02:35,570
And you also learned in the React Hooks section that if a component uses a custom hook and that custom
34
00:02:35,570 --> 00:02:40,850
hook uses you state, the component that uses to custom hook will render when you state in that custom
35
00:02:40,850 --> 00:02:42,490
hook will trigger a render.
36
00:02:42,530 --> 00:02:44,310
And that's what I'll take advantage of here.
37
00:02:45,020 --> 00:02:51,290
So I want to have some state in here in my custom hook, and that state will actually be my global state.
38
00:02:51,320 --> 00:02:55,040
So I'm using the variable which is defined outside of my hook here.
39
00:02:55,040 --> 00:02:55,750
And that's important.
40
00:02:55,760 --> 00:02:57,260
It's to find out sort of my hook.
41
00:02:57,440 --> 00:02:58,880
So it's global.
42
00:02:58,880 --> 00:03:02,750
It's not recreated when we call use store.
43
00:03:02,810 --> 00:03:07,880
It's not created separately for every component that consumes my custom hook.
44
00:03:08,090 --> 00:03:12,080
Instead, it will be created once when this file is first.
45
00:03:12,080 --> 00:03:13,520
Import that somewhere basically.
46
00:03:13,820 --> 00:03:19,280
And they're after any other file that imports from the same file will also use that same state.
47
00:03:19,280 --> 00:03:24,380
And that's one important idea here, will share data between all files that import from it.
48
00:03:24,590 --> 00:03:27,590
And that's not something we did before with custom hooks.
49
00:03:27,870 --> 00:03:31,850
The idea was the opposite, that we could share logic, but not data.
50
00:03:32,180 --> 00:03:38,450
Now we'll share logic and data by managing the data outside of the hook because instead of the hook,
51
00:03:38,450 --> 00:03:39,830
it would not be shared.
52
00:03:39,860 --> 00:03:41,900
It would be exclusive to each component.
53
00:03:41,900 --> 00:03:47,060
Each component would get its own data, but managing it outside of the hook, every file that imports
54
00:03:47,060 --> 00:03:50,660
this file or something from that file gets the same shared data.
55
00:03:51,260 --> 00:03:55,430
So here we use this state and of course.
56
00:03:57,480 --> 00:04:03,720
We therefore get our two values, which we can structure here, but I'm actually only interested in
57
00:04:03,720 --> 00:04:04,970
my set values.
58
00:04:04,980 --> 00:04:07,800
So in my set state value, you can name does whatever you want.
59
00:04:07,800 --> 00:04:13,890
But since I have that abstract global state name here, I'll just name this sad state and I just have
60
00:04:13,890 --> 00:04:18,899
to set state function because I'm only interested in the second value in the array you state gives me.
61
00:04:18,990 --> 00:04:21,440
So I'm not interested in the current state snapshot.
62
00:04:21,570 --> 00:04:24,850
I'm just interested in that second value here.
63
00:04:24,870 --> 00:04:29,490
So in the updating function now, why am I interested in this?
64
00:04:30,350 --> 00:04:35,960
I'm interested in this because whenever dysfunction is called, the component that uses my custom hook
65
00:04:36,110 --> 00:04:42,200
will render and will need this later to render our components when something in our state changes.
66
00:04:42,200 --> 00:04:43,090
We're not there yet.
67
00:04:43,100 --> 00:04:44,780
But that's why I have dysfunction.
68
00:04:45,730 --> 00:04:52,450
Now, I want to add dysfunction to my listeners, Arae, because listeners should be an array full of
69
00:04:52,450 --> 00:04:59,650
functions which I can call to update all components that are using my hook so that I have a list of
70
00:04:59,650 --> 00:05:03,700
listeners, a list of components that are interested in updates to my global state.
71
00:05:05,850 --> 00:05:13,130
So we can use listeners, push here and push, said states, so push a pointer at this function to listeners,
72
00:05:13,530 --> 00:05:18,510
that means that every component which uses my custom hook will get its own set start function, which
73
00:05:18,510 --> 00:05:21,040
is then added to the shared listeners array.
74
00:05:21,090 --> 00:05:24,780
So this array will grow over time the more components we add.
75
00:05:26,630 --> 00:05:31,310
Now, of course, it would be great that if a component is unmount, that we get rid of its listener,
76
00:05:31,310 --> 00:05:34,850
though therefore also used to use a fact hook here.
77
00:05:36,790 --> 00:05:39,040
And actually use you the fact your.
78
00:05:39,980 --> 00:05:41,660
So did I push something?
79
00:05:42,990 --> 00:05:49,310
To listeners in use of fact, so this will be ran whenever a component that uses my hook updates.
80
00:05:50,040 --> 00:05:56,070
Now I'll add that second argument to use effect to define the dependencies and with an empty array.
81
00:05:56,430 --> 00:06:02,730
This means that this effect will now only run for the component that uses my custom hook when that component
82
00:06:02,730 --> 00:06:03,330
mounts.
83
00:06:03,330 --> 00:06:04,290
And that makes sense.
84
00:06:04,290 --> 00:06:07,050
I want to register a listener for the component when it mounts.
85
00:06:07,500 --> 00:06:12,570
And we can also provide a clean up function here so that we remove the listener when the component unmount,
86
00:06:12,600 --> 00:06:18,390
because with this dependency array will simply only run the effect once and run the clean up function
87
00:06:18,390 --> 00:06:19,620
when the component is removed.
88
00:06:20,070 --> 00:06:23,130
So here we have a function where I want to remove my listener.
89
00:06:24,180 --> 00:06:24,630
For dad.
90
00:06:25,950 --> 00:06:28,860
All simply said listeners, equal to listeners.
91
00:06:29,790 --> 00:06:31,170
And filter out the listener.
92
00:06:31,200 --> 00:06:37,260
I just registered here, so here I got my listener because this function, which I pass to filter runs
93
00:06:37,260 --> 00:06:42,630
for every element and listeners array, and I want to keep all listeners that are not equal to set state
94
00:06:42,660 --> 00:06:45,480
because that state is what I added to listeners here.
95
00:06:45,840 --> 00:06:51,540
And because this is a closure here, the value of said state is captured here for that component that's
96
00:06:51,540 --> 00:06:56,940
using my custom hook and therefore will be the same when the component mounts as it is when it mounts.
97
00:06:57,150 --> 00:06:59,610
So here we can use that comparison.
98
00:06:59,760 --> 00:07:04,290
Even though that state is a function different object, it will be equal to the listener.
99
00:07:04,320 --> 00:07:07,680
So to the same sad state we registered here when the component mounted.
100
00:07:08,160 --> 00:07:10,660
So here we're removing our listener in the component amounts.
101
00:07:11,130 --> 00:07:16,620
Now, we're not entirely honest about the dependencies of use fact, though it's not actually the case
102
00:07:16,620 --> 00:07:18,200
that we have no dependencies in here.
103
00:07:18,600 --> 00:07:21,060
Instead, we do have a dependency to set state here.
104
00:07:21,390 --> 00:07:25,500
But that's no problem because that state is coming from U.S. state.
105
00:07:25,710 --> 00:07:31,800
And I mentioned in the React Hooks section that react will guarantee that this never changes for a given
106
00:07:31,800 --> 00:07:32,360
component.
107
00:07:32,580 --> 00:07:35,940
So effectively, we have only one dependency which never changes.
108
00:07:36,060 --> 00:07:39,570
So this effect will only run once and will only clean up once.
109
00:07:40,800 --> 00:07:45,120
Now, privacy, by the way, we could Amita's if we used to create a structure and syntax, then my
110
00:07:45,120 --> 00:07:49,560
limiting tool was able to find out that this is the updating function, which never changes.
111
00:07:49,740 --> 00:07:52,110
It's not able to find out if we extract it like this.
112
00:07:52,230 --> 00:07:53,540
And therefore, here we have to edit.
113
00:07:53,550 --> 00:07:55,060
But again, it will never rerun.
114
00:07:55,080 --> 00:07:56,110
So this is no problem.
115
00:07:56,790 --> 00:08:03,720
So now we're adding a set state function to our listeners for a component that uses my custom hook when
116
00:08:03,720 --> 00:08:07,010
that component mounts and removing it when it amounts.
117
00:08:08,310 --> 00:08:09,750
So the first steps are taken.
12200
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.