1 00:00:00,180 --> 00:00:07,650 Not bad, not bad ones, we have covered how we can use you state whether I think it also makes sense 2 00:00:08,130 --> 00:00:13,680 if we would take a look at how we can use with an object because there is a gotcha. 3 00:00:14,160 --> 00:00:18,540 So, again, we're looking for the set of Holder, then we're looking for use state object. 4 00:00:18,780 --> 00:00:24,840 And once we say we should see the use state object example, and of course, we just need to navigate 5 00:00:25,050 --> 00:00:32,220 to that particular file, which is in the set up again, and then use state object ajaz. 6 00:00:32,760 --> 00:00:35,280 And of course I would want to use my hook. 7 00:00:36,100 --> 00:00:37,880 So let's go with the values. 8 00:00:37,890 --> 00:00:45,240 I'm going to go with person and then that person and that is going to be equal to my use state and then 9 00:00:45,240 --> 00:00:47,420 I'll pass in that object directly. 10 00:00:47,860 --> 00:00:53,760 Again, that is something that I would want you to keep in mind where you can passing the variable like 11 00:00:53,760 --> 00:00:57,510 what you just did with an array, or we can pass in directly. 12 00:00:57,870 --> 00:01:01,550 So in this case, I'll pass in the object what it's going to be name. 13 00:01:01,920 --> 00:01:06,800 Now, that name will be Peter than age will be twenty four. 14 00:01:07,440 --> 00:01:08,620 So let's do that. 15 00:01:08,880 --> 00:01:16,290 And then lastly, we'll have some kind of message message and I'll set this up as a string so beautifully. 16 00:01:16,290 --> 00:01:17,970 I have my object. 17 00:01:18,390 --> 00:01:23,220 So let's call this a random online message so I want to receive. 18 00:01:24,030 --> 00:01:30,200 Of course, we are in good shape, and if we want to consult the person, of course, will be able to 19 00:01:30,200 --> 00:01:31,130 see our object. 20 00:01:32,120 --> 00:01:33,770 Not bad at all. 21 00:01:34,190 --> 00:01:41,360 Now, the thing is, what happens if we change this value and what God you should be looking for? 22 00:01:41,390 --> 00:01:48,400 Because with Aura, it was somewhat straightforward, where in one case we just wiped out all the data 23 00:01:48,740 --> 00:01:50,920 when we set it back to an empty right. 24 00:01:51,410 --> 00:01:57,030 And then the second thing we had the function that was just returning a new era. 25 00:01:57,560 --> 00:02:03,020 Now, this is going to be a little bit different because we'll have some values that we would want to 26 00:02:03,020 --> 00:02:03,530 preserve. 27 00:02:03,860 --> 00:02:06,720 Now, let me showcase what I'm talking about. 28 00:02:06,890 --> 00:02:11,990 I'll start with my return, where I'll set it back to a fragment. 29 00:02:12,380 --> 00:02:16,300 And then in here, let's try a few adding 3s. 30 00:02:16,640 --> 00:02:23,510 So first we'll look for person, name, person and notice, since it is an object, of course I have 31 00:02:23,510 --> 00:02:24,020 my value. 32 00:02:24,380 --> 00:02:27,800 I'm accessing my state value and then I'm just looking for a property. 33 00:02:28,160 --> 00:02:34,310 Now I will copy and paste and I'll just change these ones around where of course we'll have age here. 34 00:02:34,730 --> 00:02:39,650 We'll have also a message and let's say and there it is, Peter. 35 00:02:39,650 --> 00:02:41,240 Twenty four and a random message. 36 00:02:41,570 --> 00:02:49,810 And eventually I would want to have a button that changes the message again, button class nine for 37 00:02:49,810 --> 00:02:52,880 the stylings of Bethanne and then on Click. 38 00:02:54,200 --> 00:03:01,450 And in here, let's call the change message, change message a function that we haven't created yet. 39 00:03:01,970 --> 00:03:08,580 So in button, let's write change message and let's come up with that function. 40 00:03:09,080 --> 00:03:14,460 So in here, consed change message, that is my function. 41 00:03:14,480 --> 00:03:21,240 I'm not looking for any arguments in this case, but I would want to invoke my set person. 42 00:03:21,800 --> 00:03:27,980 Now what you'll notice though, that for example, if I want to change the message to a whole world, 43 00:03:28,430 --> 00:03:37,310 the problem is going to be that when we invoke this function, essentially we wipe out all the values. 44 00:03:37,760 --> 00:03:43,560 Remember I told you when we set up these values, the state where it can be anything that you don't 45 00:03:43,580 --> 00:03:43,840 want. 46 00:03:44,360 --> 00:03:51,080 So even though in the beginning we went with an object, if we invoke the function that changes the 47 00:03:51,080 --> 00:03:56,020 state value and we pass in a string, this will be a string. 48 00:03:56,840 --> 00:04:02,970 So originally we had an object and we're treating like an object or a person named person. 49 00:04:03,290 --> 00:04:05,360 And then the message are here. 50 00:04:06,220 --> 00:04:10,370 I just wipe out the data and then I say, you know what, it is going to be a hellhole. 51 00:04:10,870 --> 00:04:12,580 Now, you could make an argument. 52 00:04:12,580 --> 00:04:18,240 All right, but what if we would set this up as an object and I'll pass in the proper of message. 53 00:04:18,580 --> 00:04:22,480 So in this case, our same message is equal to the world. 54 00:04:22,900 --> 00:04:30,790 And technically, you are correct where now we will change only the message, but again, the rest of 55 00:04:30,790 --> 00:04:32,860 the data gets wiped out. 56 00:04:33,460 --> 00:04:34,660 So what is the solution? 57 00:04:35,020 --> 00:04:42,490 Well, the solution is using a separate operator where if you remember which operator, we first copy 58 00:04:42,490 --> 00:04:47,230 the values and then we come up with whichever value we would want to override. 59 00:04:47,590 --> 00:04:50,230 So how we would fix this in this scenario? 60 00:04:50,380 --> 00:04:53,890 I would go with that and that person and then comma. 61 00:04:54,100 --> 00:04:55,630 And then I'm just going to say, you know what? 62 00:04:55,970 --> 00:05:03,230 Leave these two values, the name and age as they are and overwrite only the message. 63 00:05:03,970 --> 00:05:05,150 So this is the gotcha. 64 00:05:05,170 --> 00:05:11,770 For example, if you're using the object, then you need to make sure that you always, always use the 65 00:05:11,770 --> 00:05:12,700 separate operator. 66 00:05:12,940 --> 00:05:21,310 Or there's also some other older setup where essentially you copy the old values from the state value 67 00:05:21,520 --> 00:05:24,940 and then you just decide which one you would want to choose. 68 00:05:25,220 --> 00:05:27,190 In this case, we want to change the whole world. 69 00:05:27,430 --> 00:05:32,590 So, of course, once we click, notice how we also change only the message.