Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,190 --> 00:00:04,430
Now let's talk about the objects
2
00:00:04,430 --> 00:00:09,340
which we are returning in our reducer, in our Redux reducer.
3
00:00:09,340 --> 00:00:12,740
I mentioned in the last lecture that we always
4
00:00:12,740 --> 00:00:17,540
return a brand new snapshot, a brand new state object
5
00:00:17,540 --> 00:00:22,020
which Redux will use to replace its existing state with.
6
00:00:22,020 --> 00:00:24,000
So the objects which we return
7
00:00:24,000 --> 00:00:28,470
in the reducer will not, and that's super important
8
00:00:28,470 --> 00:00:31,970
will not be merged with the existing state.
9
00:00:31,970 --> 00:00:35,420
They will overwrite the existing state.
10
00:00:35,420 --> 00:00:38,860
So if we, for example going set
11
00:00:38,860 --> 00:00:41,450
show counter here when we incremented
12
00:00:41,450 --> 00:00:45,620
because before it got to add it here, if we do this
13
00:00:45,620 --> 00:00:49,413
if I click increment, you see I close my counter.
14
00:00:50,440 --> 00:00:52,880
I closed my counter when I click increment even
15
00:00:52,880 --> 00:00:56,960
though I shouldn't, because I don't set show counter here
16
00:00:56,960 --> 00:00:59,900
hence it's basically removed from the object
17
00:00:59,900 --> 00:01:02,780
and it has a value of undefined therefore
18
00:01:02,780 --> 00:01:05,660
an undefined is treated as false.
19
00:01:05,660 --> 00:01:08,120
So we don't render to counter then.
20
00:01:08,120 --> 00:01:10,145
And that's of course a side-effect, which we don't want.
21
00:01:10,145 --> 00:01:11,623
That's an error.
22
00:01:12,530 --> 00:01:16,900
So we must always set all the other states
23
00:01:16,900 --> 00:01:19,110
when we update a piece of state
24
00:01:20,000 --> 00:01:22,513
because we overwrite the old state.
25
00:01:23,710 --> 00:01:25,670
Now that brings up one question.
26
00:01:25,670 --> 00:01:29,330
Why do we need to return a new piece of data here?
27
00:01:29,330 --> 00:01:31,950
Why can't we just use the state
28
00:01:31,950 --> 00:01:35,600
which we're getting as an argument, access counter
29
00:01:35,600 --> 00:01:38,763
and increment it like this, instead of returning?
30
00:01:39,650 --> 00:01:43,610
Or maybe we do it like this, and then we return state.
31
00:01:43,610 --> 00:01:46,740
And when we do that, we changed state before we return it.
32
00:01:46,740 --> 00:01:48,693
And then we just return it again.
33
00:01:49,530 --> 00:01:54,530
Well, if we do that and we reload, everything works.
34
00:01:55,490 --> 00:01:58,060
So it's not easy to see that this is wrong,
35
00:01:58,060 --> 00:02:00,820
but it is, even though it works.
36
00:02:00,820 --> 00:02:05,180
This is something you absolutely must not do
37
00:02:05,180 --> 00:02:07,210
when working with Redux.
38
00:02:07,210 --> 00:02:10,800
You should never super important never
39
00:02:10,800 --> 00:02:13,600
mutate the state, the existing state.
40
00:02:13,600 --> 00:02:16,670
You should never change the existing state.
41
00:02:16,670 --> 00:02:19,560
Instead, always override it
42
00:02:19,560 --> 00:02:23,200
by returning a brand new state object.
43
00:02:23,200 --> 00:02:27,240
And because objects and arrays are reference values
44
00:02:27,240 --> 00:02:31,270
in JavaScript, it's easy to accidentally override
45
00:02:31,270 --> 00:02:33,550
and change the existing state.
46
00:02:33,550 --> 00:02:35,580
You could for example think that
47
00:02:35,580 --> 00:02:37,510
you can do something like this.
48
00:02:37,510 --> 00:02:40,590
And then you return a new object where you then
49
00:02:40,590 --> 00:02:44,290
do set to counter to state.counter
50
00:02:44,290 --> 00:02:47,573
and show counter to state.showcounter.
51
00:02:48,870 --> 00:02:51,720
We can do this because we changed the counter here.
52
00:02:51,720 --> 00:02:53,880
And then we returned a brand new object.
53
00:02:53,880 --> 00:02:55,780
Everything's good, right?
54
00:02:55,780 --> 00:02:57,540
Well, again, that will work.
55
00:02:57,540 --> 00:03:01,210
If I reload, it works, but it's still super bad
56
00:03:01,210 --> 00:03:06,000
because we still mutate the existing state because objects
57
00:03:06,000 --> 00:03:09,990
and arrays are reference values in JavaScript.
58
00:03:09,990 --> 00:03:13,220
And in case it's not clear to you What I mean by that.
59
00:03:13,220 --> 00:03:16,010
Attached you find a link which leads you
60
00:03:16,010 --> 00:03:20,033
to an article and a video where I explained this in depth.
61
00:03:20,900 --> 00:03:23,410
Now, when working with redux and it's state
62
00:03:23,410 --> 00:03:26,280
you must never mutate state like this
63
00:03:26,280 --> 00:03:29,760
never changed the original state which you're getting.
64
00:03:29,760 --> 00:03:33,400
This can lead to bugs, unpredictable behavior
65
00:03:33,400 --> 00:03:37,500
and it can make debugging your application harder as well.
66
00:03:37,500 --> 00:03:40,130
So even though it doesn't lead to a bug here
67
00:03:40,130 --> 00:03:43,470
it can have unwanted and unexpected side effects
68
00:03:43,470 --> 00:03:47,680
in bigger applications where your state gets out of sync.
69
00:03:47,680 --> 00:03:51,460
And suddenly the UI is not reflecting your state
70
00:03:51,460 --> 00:03:52,720
correctly anymore.
71
00:03:52,720 --> 00:03:55,380
And hence the simple rule is never
72
00:03:55,380 --> 00:03:57,060
mutate your state like this.
73
00:03:57,060 --> 00:03:59,630
Always return a brand new object
74
00:03:59,630 --> 00:04:03,060
where you copy any nested objects or erase.
75
00:04:03,060 --> 00:04:06,870
If you have any and create brand new values
76
00:04:06,870 --> 00:04:08,510
as we're doing it here.
77
00:04:08,510 --> 00:04:10,680
By updating our state like this
78
00:04:10,680 --> 00:04:15,100
we create a brand new object where we don't change anything.
79
00:04:15,100 --> 00:04:17,519
And especially when you have a state
80
00:04:17,519 --> 00:04:19,750
with nested objects and arrays
81
00:04:19,750 --> 00:04:22,890
it's easy to accidentally mutate your existing state.
82
00:04:22,890 --> 00:04:25,120
And therefore you should be super careful
83
00:04:25,120 --> 00:04:27,550
that you do this in an immutable way
84
00:04:27,550 --> 00:04:31,200
that you'd never accidentally mutate your existing state
85
00:04:31,200 --> 00:04:34,300
but you copy any objects which you added.
86
00:04:34,300 --> 00:04:37,953
So you always create a brand new object or array.
87
00:04:37,953 --> 00:04:40,640
Whenever you need to update data
88
00:04:40,640 --> 00:04:43,660
you never just dive into an existing object
89
00:04:43,660 --> 00:04:46,640
and start manipulating its properties.
90
00:04:46,640 --> 00:04:50,480
Always copy and create new objects.
91
00:04:50,480 --> 00:04:53,110
Now, at this point it might look a little bit too early
92
00:04:53,110 --> 00:04:54,760
to emphasize it like this,
93
00:04:54,760 --> 00:04:57,330
because this is a fairly simple state here
94
00:04:57,330 --> 00:04:59,960
but it is super important, easy to mess up
95
00:04:59,960 --> 00:05:02,830
and something you should know right from the start
96
00:05:02,830 --> 00:05:05,343
which is why I am emphasizing it here.
7733
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.