Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,140 --> 00:00:05,430
Now let's actually stay on this form page.
2
00:00:05,430 --> 00:00:07,530
If I'm the user of this page,
3
00:00:07,530 --> 00:00:09,860
and I started entering something
4
00:00:09,860 --> 00:00:13,560
like test and I started entering some text here,
5
00:00:13,560 --> 00:00:16,730
but then I accidentally swiped back
6
00:00:16,730 --> 00:00:19,180
or pressed the back button
7
00:00:19,180 --> 00:00:23,290
then I just go away and all my state is lost.
8
00:00:23,290 --> 00:00:25,500
Whatever I entered here is lost.
9
00:00:25,500 --> 00:00:28,510
Now you might've noticed that on some web pages,
10
00:00:28,510 --> 00:00:30,740
where you can enter data in a form
11
00:00:30,740 --> 00:00:33,020
this is prevented by a prompt.
12
00:00:33,020 --> 00:00:34,970
Which is shown to you asking you
13
00:00:34,970 --> 00:00:37,360
if you really wanna navigate away.
14
00:00:37,360 --> 00:00:40,060
So that as soon as you started working on that form,
15
00:00:40,060 --> 00:00:43,080
you can't accidentally leave it.
16
00:00:43,080 --> 00:00:46,260
And that's a behavior we also might want to implement.
17
00:00:46,260 --> 00:00:50,000
And React Router also makes that very simple.
18
00:00:50,000 --> 00:00:53,770
Cue implemented, we can go to this quote form component
19
00:00:53,770 --> 00:00:58,320
where we have this form and we wanna do two things.
20
00:00:58,320 --> 00:01:01,160
First, we wanna determine when the user starts working
21
00:01:01,160 --> 00:01:05,239
with this form, for example, when this form gains focus.
22
00:01:05,239 --> 00:01:09,200
Then as a second step, we wanna show a warning to the user,
23
00:01:09,200 --> 00:01:12,990
if he or she tries to leave the page after
24
00:01:12,990 --> 00:01:14,633
starting to work on that form.
25
00:01:15,820 --> 00:01:18,160
Now to find out if the form is active,
26
00:01:18,160 --> 00:01:22,083
we can listen to the on focus event here.
27
00:01:23,550 --> 00:01:25,900
Or we can set this on focus prop
28
00:01:25,900 --> 00:01:30,000
and then trigger some function when this form gains focus.
29
00:01:30,000 --> 00:01:32,950
The form focused handler
30
00:01:32,950 --> 00:01:35,200
could be the function which we wanna trigger.
31
00:01:36,040 --> 00:01:38,560
So that's the functional connect here.
32
00:01:38,560 --> 00:01:43,560
If I just console log focus here in this function
33
00:01:43,900 --> 00:01:47,210
then you will notice in the dev tools,
34
00:01:47,210 --> 00:01:50,830
that if I reload this page, we don't see focus.
35
00:01:50,830 --> 00:01:54,670
But if I click anywhere on this form, then I do get this.
36
00:01:54,670 --> 00:01:56,490
If I click into this input here
37
00:01:56,490 --> 00:01:59,713
I do get focused, locked to the console.
38
00:02:01,150 --> 00:02:04,053
So this is how this form then becomes active.
39
00:02:05,180 --> 00:02:07,570
Now in this form focused handler, we now
40
00:02:07,570 --> 00:02:12,150
wanna store that information that's this form was focused.
41
00:02:12,150 --> 00:02:13,850
And we then wanna show a warning,
42
00:02:13,850 --> 00:02:15,993
if the user tries to navigate away.
43
00:02:16,870 --> 00:02:20,280
Now for storing information, that the forum was focused
44
00:02:20,280 --> 00:02:22,290
we can use state.
45
00:02:22,290 --> 00:02:26,980
We can import use state hook from react,
46
00:02:26,980 --> 00:02:31,683
and add a new piece of state here in this component.
47
00:02:34,010 --> 00:02:35,680
Initially, it's false maybe
48
00:02:35,680 --> 00:02:40,210
because this is the ease entering state, maybe
49
00:02:40,210 --> 00:02:44,280
set is entering because the users started entering data
50
00:02:44,280 --> 00:02:47,113
or at least the user started working on that form.
51
00:02:48,260 --> 00:02:50,490
Now one set is entering to true
52
00:02:50,490 --> 00:02:53,360
if this form focused handler fires.
53
00:02:53,360 --> 00:02:54,570
But then I'll set this to true
54
00:02:54,570 --> 00:02:56,610
and change this state.
55
00:02:56,610 --> 00:03:00,750
With that we now know once user is working on the form.
56
00:03:00,750 --> 00:03:02,470
Showing a warning to the user,
57
00:03:02,470 --> 00:03:05,000
when the user then tries to leave that form
58
00:03:05,000 --> 00:03:06,010
maybe accidentally,
59
00:03:06,010 --> 00:03:09,020
is then routered straight forward actually.
60
00:03:09,020 --> 00:03:11,300
Because for this react router,
61
00:03:11,300 --> 00:03:14,520
has a special component which we can use.
62
00:03:14,520 --> 00:03:17,600
So from react router Dom, we can import something.
63
00:03:17,600 --> 00:03:20,683
And that something is the prompt component.
64
00:03:21,650 --> 00:03:24,600
This is a component which we can render.
65
00:03:24,600 --> 00:03:27,580
And then this component will automatically watch
66
00:03:27,580 --> 00:03:29,110
if we navigate away.
67
00:03:29,110 --> 00:03:31,700
And if then a certain condition is met,
68
00:03:31,700 --> 00:03:34,953
it will show a warning before it allows us to leave.
69
00:03:35,970 --> 00:03:38,743
Now for this, we need to render this component.
70
00:03:39,960 --> 00:03:42,640
And for that we can actually use a fragment
71
00:03:42,640 --> 00:03:45,160
to wrap this card.
72
00:03:45,160 --> 00:03:47,910
And then side by side to this card,
73
00:03:47,910 --> 00:03:52,133
render this prompt component as a self-closing component.
74
00:03:53,080 --> 00:03:56,200
Now prompt needs two props.
75
00:03:56,200 --> 00:04:00,630
It needs a when prop where we pass in true or false,
76
00:04:00,630 --> 00:04:03,200
to finding whether this prompt should be shown
77
00:04:03,200 --> 00:04:06,250
if the user changes the URL or not.
78
00:04:06,250 --> 00:04:10,470
And I wanna show it, if is entering is true.
79
00:04:10,470 --> 00:04:13,460
And that should be is entering here by the way.
80
00:04:13,460 --> 00:04:14,490
So is entering.
81
00:04:14,490 --> 00:04:18,240
If is entering is true, then I wanna show this prompt
82
00:04:18,240 --> 00:04:20,632
if the user tries to leave the page.
83
00:04:21,800 --> 00:04:25,030
We can also set a message prop then,
84
00:04:25,030 --> 00:04:28,930
which actually takes a function which should return a string
85
00:04:28,930 --> 00:04:31,110
with the text we wanna show to the user
86
00:04:31,110 --> 00:04:33,530
when he or she tries to leave.
87
00:04:33,530 --> 00:04:36,950
This is a function because we get some location object here,
88
00:04:36,950 --> 00:04:39,100
which holds information about the page
89
00:04:39,100 --> 00:04:41,010
we're trying to go to.
90
00:04:41,010 --> 00:04:43,270
This would allow us to include the path
91
00:04:43,270 --> 00:04:45,610
we're trying to go to in this message
92
00:04:45,610 --> 00:04:47,410
which we're showing.
93
00:04:47,410 --> 00:04:49,370
But here I don't care about this location.
94
00:04:49,370 --> 00:04:53,440
I'll just say are you sure you want to leave?
95
00:04:53,440 --> 00:04:58,063
All your entered data will be lost.
96
00:04:58,940 --> 00:05:01,210
And that's the message which I'll show to the user
97
00:05:01,210 --> 00:05:03,733
when he or she tries to leave this page.
98
00:05:05,300 --> 00:05:07,410
With that if we now save this,
99
00:05:07,410 --> 00:05:09,990
if I reload this page and I go to a different page
100
00:05:09,990 --> 00:05:13,110
this works. Because I haven't touched the form yet.
101
00:05:13,110 --> 00:05:16,380
If I do touch the form though, and I then, for example,
102
00:05:16,380 --> 00:05:20,170
click on all quotes, I do get this prompt though.
103
00:05:20,170 --> 00:05:22,340
Here we see the message we entered.
104
00:05:22,340 --> 00:05:26,260
And if I click cancel, we stay on this page.
105
00:05:26,260 --> 00:05:29,170
Of course I can still leave by clicking okay,
106
00:05:29,170 --> 00:05:30,560
so I can override this.
107
00:05:30,560 --> 00:05:32,700
But I get this extra warning,
108
00:05:32,700 --> 00:05:35,060
if I started working on the form
109
00:05:35,060 --> 00:05:38,773
to ensure that I don't accidentally lose my entered data.
110
00:05:39,910 --> 00:05:43,670
At the moment we just have a small flaw here, though.
111
00:05:43,670 --> 00:05:45,400
Even if I do enter data
112
00:05:45,400 --> 00:05:48,430
and click add quote, I'll get this warning.
113
00:05:48,430 --> 00:05:51,480
And that's of course not the desired behavior.
114
00:05:51,480 --> 00:05:54,770
And to ensure that we don't get this warning in this case,
115
00:05:54,770 --> 00:05:58,860
we could, for example, add an extra click listener here
116
00:05:58,860 --> 00:06:01,710
on this button which submits the form
117
00:06:01,710 --> 00:06:05,990
and trigger to finish entering handler function
118
00:06:05,990 --> 00:06:08,220
which we have yet to add.
119
00:06:08,220 --> 00:06:11,590
And then define this extra function.
120
00:06:11,590 --> 00:06:14,110
The finish entering handler function here
121
00:06:14,950 --> 00:06:19,490
and here I then call set is entering and set this to false
122
00:06:19,490 --> 00:06:22,253
so that I do not block transitions anymore.
123
00:06:23,150 --> 00:06:25,163
And with that, if we save this,
124
00:06:26,250 --> 00:06:28,310
now we can leave that page.
125
00:06:28,310 --> 00:06:30,350
So if I do not click add quote
126
00:06:30,350 --> 00:06:32,840
and the form has focus, I get the warning.
127
00:06:32,840 --> 00:06:35,650
If I do enter something here though,
128
00:06:35,650 --> 00:06:37,500
and I then click this button,
129
00:06:37,500 --> 00:06:39,253
then I can navigate away.
130
00:06:40,230 --> 00:06:42,800
Setting set is entering two false inside
131
00:06:42,800 --> 00:06:45,170
of the submit form handler would not work
132
00:06:45,170 --> 00:06:47,270
because that's a little bit too late.
133
00:06:47,270 --> 00:06:50,350
This state update would not go through,
134
00:06:50,350 --> 00:06:53,500
before we actually triggered a navigation action.
135
00:06:53,500 --> 00:06:55,650
Because the navigation action will be triggered
136
00:06:55,650 --> 00:06:59,640
in on add quote. So that's all one synchronous process.
137
00:06:59,640 --> 00:07:01,200
It's one function in the end
138
00:07:01,200 --> 00:07:03,880
and hence the state update would not be processed
139
00:07:03,880 --> 00:07:06,180
before we try to navigate away.
140
00:07:06,180 --> 00:07:09,750
That's why I'm using a separate function for that here
141
00:07:09,750 --> 00:07:11,790
which triggers when the button is clicked
142
00:07:11,790 --> 00:07:15,973
and therefore before we actually handle the form submission.
143
00:07:16,990 --> 00:07:19,310
And with that we're also using this
144
00:07:19,310 --> 00:07:21,640
prompt component react router gives us
145
00:07:21,640 --> 00:07:24,983
to ensure that we prevent unwanted transitions.
11575
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.