Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,164 --> 00:00:03,540
[Maximilian Schwarzmuller] Now this demo application
2
00:00:03,540 --> 00:00:04,960
is taking shape.
3
00:00:04,960 --> 00:00:08,450
Let's now take look at this, Add a Quote form.
4
00:00:08,450 --> 00:00:12,330
Now we will later at the actual logic for adding a quote
5
00:00:12,330 --> 00:00:14,950
for the moment, If we open the dev tools,
6
00:00:14,950 --> 00:00:16,460
if I
7
00:00:16,460 --> 00:00:18,220
enter some data here,
8
00:00:18,220 --> 00:00:20,560
we just see it locked to the console because
9
00:00:20,560 --> 00:00:23,053
that's all we're doing upon a form submission.
10
00:00:23,900 --> 00:00:25,310
But let's imagine
11
00:00:25,310 --> 00:00:28,290
that this actually would be sent to a server.
12
00:00:28,290 --> 00:00:32,430
What would we typically expect from an application
13
00:00:32,430 --> 00:00:35,520
from a website if we submitted a form.
14
00:00:35,520 --> 00:00:38,880
We typically expect either some success message,
15
00:00:38,880 --> 00:00:41,950
maybe a modal overlay, which opens up,
16
00:00:41,950 --> 00:00:45,750
or we expect that we navigate somewhere else.
17
00:00:45,750 --> 00:00:47,840
If I add a new quote
18
00:00:47,840 --> 00:00:51,130
and that request was sent to the server,
19
00:00:51,130 --> 00:00:52,740
I might expect
20
00:00:52,740 --> 00:00:56,700
that I'm automatically navigated away from this page,
21
00:00:56,700 --> 00:00:59,350
maybe to the All Quotes page.
22
00:00:59,350 --> 00:01:00,370
And that is something which
23
00:01:00,370 --> 00:01:02,623
I do wanna implement here as well.
24
00:01:03,590 --> 00:01:05,150
Now for that of course,
25
00:01:05,150 --> 00:01:07,930
we could convert our submit button here
26
00:01:07,930 --> 00:01:10,810
in the QuoteForm to a link so
27
00:01:10,810 --> 00:01:13,680
that it links to the All Quote page.
28
00:01:13,680 --> 00:01:15,550
But actually here we don't want the link,
29
00:01:15,550 --> 00:01:18,040
we want a button which submits the form.
30
00:01:18,040 --> 00:01:21,180
So using a link is not an option here.
31
00:01:21,180 --> 00:01:22,510
Instead, what we need here
32
00:01:22,510 --> 00:01:26,090
is something called programmatic navigation.
33
00:01:26,090 --> 00:01:29,000
We wanna trigger a navigation action
34
00:01:29,000 --> 00:01:32,840
and navigate the user away programmatically
35
00:01:32,840 --> 00:01:34,560
in our code.
36
00:01:34,560 --> 00:01:37,900
So it's not a link which the user clicks to navigate away,
37
00:01:37,900 --> 00:01:40,870
but it's some action triggered by our code,
38
00:01:40,870 --> 00:01:43,500
when some abreaction sending the
39
00:01:43,500 --> 00:01:46,023
entered data to a server finished.
40
00:01:46,950 --> 00:01:49,510
And in the end we probably wanna trigger
41
00:01:49,510 --> 00:01:51,660
this navigation action from inside,
42
00:01:51,660 --> 00:01:54,550
the NewQuote page component here.
43
00:01:54,550 --> 00:01:57,690
Because here we do have this addQuoteHandler
44
00:01:57,690 --> 00:02:01,000
where we will later send the requests to the server,
45
00:02:01,000 --> 00:02:05,070
and therefore it's here where we also want to navigate away.
46
00:02:05,070 --> 00:02:08,560
But how can we now navigate programmatically?
47
00:02:08,560 --> 00:02:12,483
How can we tell React Router that we wanna switch pages?
48
00:02:13,330 --> 00:02:17,030
Well for this React Router again has a hook,
49
00:02:17,030 --> 00:02:18,910
which we can import.
50
00:02:18,910 --> 00:02:21,890
So we can import from react-router-dom,
51
00:02:21,890 --> 00:02:25,900
and we can import the useHistory hook.
52
00:02:25,900 --> 00:02:27,610
The name might be a bit strange,
53
00:02:27,610 --> 00:02:29,910
but it's named like this because it allows us
54
00:02:29,910 --> 00:02:32,730
to change the browser history.
55
00:02:32,730 --> 00:02:35,803
So the history of pages we visited.
56
00:02:36,690 --> 00:02:39,000
We call useHistory like this,
57
00:02:39,000 --> 00:02:41,770
and that gives us a history object,
58
00:02:41,770 --> 00:02:44,020
which we can store in a constant.
59
00:02:44,020 --> 00:02:46,640
And extend this history object,
60
00:02:46,640 --> 00:02:47,770
which we can use
61
00:02:47,770 --> 00:02:48,603
to
62
00:02:48,603 --> 00:02:52,860
trigger certain history changing actions.
63
00:02:52,860 --> 00:02:55,520
And what changes the history of pages,
64
00:02:55,520 --> 00:02:57,870
well, for example if we add a new page,
65
00:02:57,870 --> 00:03:00,220
if we go to a new page.
66
00:03:00,220 --> 00:03:01,800
And we can navigate around
67
00:03:01,800 --> 00:03:03,970
with the push method here,
68
00:03:03,970 --> 00:03:07,440
which pushes a new page on the stack of pages,
69
00:03:07,440 --> 00:03:11,250
so a new page on our history of pages,
70
00:03:11,250 --> 00:03:13,940
or we can navigate with the replace method
71
00:03:13,940 --> 00:03:16,600
that replaces the current page.
72
00:03:16,600 --> 00:03:18,850
The difference is that with push,
73
00:03:18,850 --> 00:03:21,540
we can go back with the back button
74
00:03:21,540 --> 00:03:23,350
to the page we're coming from,
75
00:03:23,350 --> 00:03:25,650
with replace we can't.
76
00:03:25,650 --> 00:03:28,000
So replace is like a redirect
77
00:03:28,000 --> 00:03:32,660
where we changed occurred page, push adds a new page.
78
00:03:32,660 --> 00:03:34,180
And it's up to you what you want.
79
00:03:34,180 --> 00:03:37,580
Do you want to allow your user to go back or not
80
00:03:37,580 --> 00:03:39,090
Here I'll go for push
81
00:03:39,090 --> 00:03:41,500
and I will allow the user to go back,
82
00:03:41,500 --> 00:03:45,930
and then here we could go to /quotes like this.
83
00:03:45,930 --> 00:03:48,120
So now we will navigate away
84
00:03:48,120 --> 00:03:50,830
if we send our data here
85
00:03:50,830 --> 00:03:51,920
at the moment, of course,
86
00:03:51,920 --> 00:03:54,123
we're only logging it to the console.
87
00:03:54,970 --> 00:03:57,160
But with that if we now save this,
88
00:03:57,160 --> 00:03:59,320
if I enter some
89
00:03:59,320 --> 00:04:00,600
data here,
90
00:04:00,600 --> 00:04:01,970
this is a test,
91
00:04:01,970 --> 00:04:03,840
and I click Add Quote,
92
00:04:03,840 --> 00:04:05,480
you see it's locked to the console,
93
00:04:05,480 --> 00:04:08,140
but we're also navigated away.
94
00:04:08,140 --> 00:04:11,490
And that is how we can implement programmatic navigation
95
00:04:11,490 --> 00:04:13,133
with React Router.
6975
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.