1 00:00:02,370 --> 00:00:08,100 Now with the filters rendered here, of course it would be nice to have a save button here at the top that 2 00:00:08,100 --> 00:00:11,840 allows us to save any changes to filters we made. 3 00:00:11,940 --> 00:00:16,530 So in the filters screen, we already have one header button on headerLeft, 4 00:00:16,530 --> 00:00:23,430 I now also want to have one on headerRight, so on the right of my header. So headerRight here is basically 5 00:00:23,430 --> 00:00:28,050 the same as we have it here, so we can just copy this here because we want to have header buttons there 6 00:00:28,050 --> 00:00:35,280 with one button, that will be the save button and the icon we can use there is for example and of course 7 00:00:35,280 --> 00:00:41,250 you can pick a different one but here, I will go with for example save and now when this is pressed however, 8 00:00:41,400 --> 00:00:44,940 that will be a bit tricky. Of course, we can fire a function and 9 00:00:44,940 --> 00:00:48,710 we could console log saving filters here. 10 00:00:48,900 --> 00:00:54,000 Now the actual logic for saving and applying filters is something we'll take care about in the next 11 00:00:54,120 --> 00:00:57,150 module but still, there is some issue with that. 12 00:00:57,720 --> 00:01:01,530 If I save it like this and we go to the filters, of course we can press this, 13 00:01:01,530 --> 00:01:12,150 this works but what would be nice is that we then really gather our current state of filters, so basically 14 00:01:12,150 --> 00:01:19,170 get a snapshot of our current state here and for the moment log this to the console, in the next module 15 00:01:19,320 --> 00:01:26,740 apply this somehow to filter the recipes we're showing on the other screens. Now this might sound 16 00:01:26,740 --> 00:01:28,910 trivial but we'll have a problem here, 17 00:01:29,110 --> 00:01:36,360 our filters, so the information, which filters are set, that's all stored in these state variables here, right 18 00:01:36,370 --> 00:01:38,560 and that's part of our component, 19 00:01:38,770 --> 00:01:41,530 our button however is in the navigation options 20 00:01:41,530 --> 00:01:47,440 and yes, there we get the snap data which for example gives us the navigation prop in the end but what 21 00:01:47,450 --> 00:01:52,990 we don't have here is access to the state of our component and that is what I need here though 22 00:01:52,990 --> 00:01:57,970 and now this here is really an important pattern which might look like a strange hack or workaround 23 00:01:58,540 --> 00:02:08,020 but which is the way of actually communicating between your navigation options and your component 24 00:02:08,020 --> 00:02:13,150 if you depend on changing data in your component inside of your navigation options. So in the navigation 25 00:02:13,150 --> 00:02:18,830 options, you need information about data that changed in your component. 26 00:02:18,840 --> 00:02:24,980 This is also an approach you find in the official docs of the navigation by the way. We can use params, 27 00:02:25,190 --> 00:02:30,940 so that thing we pass between screens, to communicate between our component and the navigation options 28 00:02:31,010 --> 00:02:35,070 and this might sound strange but this is exactly what we'll do now. 29 00:02:36,360 --> 00:02:44,170 Let's say here in our component, we add a new function and I'll name this function here save filters, 30 00:02:44,460 --> 00:02:46,950 you can name it however you want. 31 00:02:47,000 --> 00:02:49,970 It's a function that takes no arguments but that has one job, 32 00:02:51,110 --> 00:02:57,410 it will create an applied filters constant for example which is an object which gathers our filters. 33 00:02:57,410 --> 00:03:02,300 So there we could have glutenFree as a key and we store isGlutenFree as a value, 34 00:03:02,300 --> 00:03:07,250 we have lactoseFree as a key and we store our current isLactoseFree 35 00:03:07,250 --> 00:03:08,480 state value. 36 00:03:08,690 --> 00:03:17,540 We do the same for vegan with isVegan and we do the same for isVegetarian and isVegetarian, 37 00:03:17,540 --> 00:03:23,930 like that and thereafter, I will console log this applied filters thing. 38 00:03:24,070 --> 00:03:32,770 Now the thing is, I would like to give access to this function which is part of my component to my navigation 39 00:03:32,770 --> 00:03:38,080 options so that I can trigger this function from inside the navigation options and for this, we can use 40 00:03:38,200 --> 00:03:38,890 params. 41 00:03:42,490 --> 00:03:49,300 Let's add use effect so that we have a way of executing code whenever our state changes because that's 42 00:03:49,300 --> 00:03:58,810 when I want to forward this updated function which basically captures my current state to my navigation 43 00:03:58,810 --> 00:03:59,830 options. 44 00:03:59,830 --> 00:04:06,280 So here, I can add use effect and use effect takes a function which runs whenever our state changed and 45 00:04:06,280 --> 00:04:16,460 whatever the component re-rendered and in there, we can now use props navigation set params. 46 00:04:16,460 --> 00:04:18,130 We haven't used set params before, 47 00:04:18,130 --> 00:04:20,400 we only worked with get param. 48 00:04:20,510 --> 00:04:27,170 We didn't use set params because before we only set params when we navigated to a new screen, then for 49 00:04:27,170 --> 00:04:33,530 example as in the categories screen, we passed our params like this to the new screen but we can 50 00:04:33,560 --> 00:04:42,160 use set params to update the params values for the currently loaded screen. Now here on this screen, 51 00:04:42,160 --> 00:04:44,600 we got no params, so it's empty, 52 00:04:45,690 --> 00:04:51,120 hence we can set params to a brand new object here and I want to add one entry there but you could 53 00:04:51,120 --> 00:04:52,810 add as many as you want. 54 00:04:53,780 --> 00:05:04,040 I want to add a save key and you can take any name for that key which you want and point at save filters, 55 00:05:04,060 --> 00:05:08,520 so at this function. I don't execute the function, I don't add parentheses, 56 00:05:08,580 --> 00:05:10,050 I just point at it, 57 00:05:10,080 --> 00:05:16,740 I just use this variable which holds a pointer at this function in the end and forward this value, so forward 58 00:05:16,740 --> 00:05:23,250 this pointer or store this pointer to that function in that save key, in that object which I now set as 59 00:05:23,250 --> 00:05:28,230 params in this screen, in the filters screen. 60 00:05:28,430 --> 00:05:34,280 This allows us to go to the navigation options and there we have access to nav data and therefore to 61 00:05:34,340 --> 00:05:43,310 navigation right and that means that here for the save button, when pressing this, we can actually use 62 00:05:43,310 --> 00:05:46,790 navData.navigation.getParam, 63 00:05:46,790 --> 00:05:53,060 that is how we can retrieve a parameter and that's available here in navigation options too, save and I can 64 00:05:53,060 --> 00:06:03,270 retrieve that save parameter which I'm setting here in use effect and I know this is super strange 65 00:06:03,280 --> 00:06:08,120 the first time you see this and it looks like a really dirty hack but it is not. 66 00:06:08,200 --> 00:06:14,230 This is a valid way of communicating between your component and your navigation options which you typically 67 00:06:14,230 --> 00:06:18,300 need when having action items in your navigation options. 68 00:06:18,310 --> 00:06:26,880 This will get us access to our parameters and there we access the save parameter, the save parameter 69 00:06:27,600 --> 00:06:33,180 will be this function and we update the value in that parameter whenever our state changes and we need 70 00:06:33,180 --> 00:06:38,880 to do this because in this function, this is recreated whenever our state changes and it logs in the 71 00:06:38,880 --> 00:06:40,530 latest state values 72 00:06:40,620 --> 00:06:45,420 and that's of course what we want when this function gets executed eventually with the help of params 73 00:06:45,750 --> 00:06:49,110 through that button in the navigation options. 74 00:06:49,110 --> 00:06:54,300 So we use params as a way of communicating between our component and the navigation options, 75 00:06:54,300 --> 00:06:59,730 kind of a workaround but indeed, the one you find in the official docs as well and the way of how you 76 00:06:59,730 --> 00:07:07,390 communicate here. Now before we save this, there are two things we need to do though, use effect right now 77 00:07:07,390 --> 00:07:14,320 runs whenever this component updates. In the end, it should only run when save filters hold a new value. 78 00:07:14,440 --> 00:07:20,740 So I'll add the second argument to use effect which is this array of dependencies and there, save filters, 79 00:07:20,980 --> 00:07:23,530 so this variable here which holds a function is a dependency 80 00:07:23,530 --> 00:07:26,470 and right now, this will always rebuild when you component rebuild, 81 00:07:26,470 --> 00:07:30,900 so it's not really a dependency that helps us but we'll soon fix this 82 00:07:31,090 --> 00:07:37,600 and props also is a dependency. Now actually to avoid unnecessary re-renders whenever anything in the 83 00:07:37,600 --> 00:07:39,140 param component changes, 84 00:07:39,370 --> 00:07:48,330 I'll use a different way of extracting my navigation prop, I'll use destructuring here, like this, navigation 85 00:07:48,870 --> 00:07:49,770 equals props. 86 00:07:49,770 --> 00:07:56,350 This is a syntax which uses object destructuring which means props is an object and this pulls out the 87 00:07:56,350 --> 00:08:02,460 navigation key and stores this in a brand new constant of the same name, so of the same navigation. It stores 88 00:08:02,460 --> 00:08:05,560 the value in the navigation prop in that navigation constant 89 00:08:05,560 --> 00:08:11,940 now, that's what the syntax does and the advantages that we now have a navigation constant here which 90 00:08:11,940 --> 00:08:17,780 we can use in use effect without props because we have this stored in a separate constant now 91 00:08:17,820 --> 00:08:21,860 and now we can add this as a dependency which means when this changes, this wil rebuild 92 00:08:21,990 --> 00:08:27,500 but if anything else in the props changes, this will not unnecessarily rerun the effect. 93 00:08:27,750 --> 00:08:34,350 Now to make sure that save filters only updates when our state changes, we can import the use callback 94 00:08:34,350 --> 00:08:38,000 hook from React, another hook built into React 95 00:08:38,080 --> 00:08:43,560 and this allows us to wrap a function so that this function is actually cached by React and is only 96 00:08:43,560 --> 00:08:46,710 recreated if its dependencies changed. 97 00:08:46,710 --> 00:08:53,610 We wrap this around our save filters functions, just like this, so we pass this function as an argument 98 00:08:53,610 --> 00:08:56,010 to use callback and use callback 99 00:08:56,010 --> 00:09:01,710 also takes a second argument which is an array of dependencies and there, we need to specify all 100 00:09:01,710 --> 00:09:03,300 the dependencies we have here 101 00:09:03,300 --> 00:09:09,930 that could change and that would lead us to recreate that function and that essentially is the case 102 00:09:09,930 --> 00:09:18,410 if one of the four states here changes. So isGlutenFree, isLactoseFree, isVegan and isVegetarian 103 00:09:18,420 --> 00:09:23,610 are now all dependencies of use callback and that means this component function here will be recreated 104 00:09:23,730 --> 00:09:26,160 if any of these states here changes, 105 00:09:26,310 --> 00:09:32,210 if anything else causes this component to re-render, we'll not recreate this function 106 00:09:32,310 --> 00:09:37,410 and that's in turn important because that save filters function is a dependency of use effect. 107 00:09:37,530 --> 00:09:43,620 So if that is recreated, use effect runs again which updates our params and we want to keep this to 108 00:09:43,620 --> 00:09:48,340 a minimum and not constantly update our params but really only do that if we need to do it. 109 00:09:49,950 --> 00:09:56,790 With this in place, we should now be able to press the save button and see the filters we chose, 110 00:09:56,850 --> 00:09:59,250 so let's go to filters here 111 00:10:01,970 --> 00:10:03,770 and now this breaks. 112 00:10:03,770 --> 00:10:06,000 Do you have any idea why it would break? 113 00:10:06,980 --> 00:10:14,380 The problem is, I have a navigation here as a dependency in use effect and I explained why I have 114 00:10:14,380 --> 00:10:20,590 it, right, that I don't want to have props in its entirety and so on. A problem just is, when we call set 115 00:10:20,590 --> 00:10:21,090 params, 116 00:10:21,100 --> 00:10:27,970 we add new params to our navigator in the end and what this also does is it causes navigation to 117 00:10:27,970 --> 00:10:28,770 change. 118 00:10:28,900 --> 00:10:31,150 So in the end, I have an infinite loop here. 119 00:10:31,300 --> 00:10:36,810 If I remove navigation here as a dependency, then this will work. If we now save this, 120 00:10:36,820 --> 00:10:42,550 now if you close both apps here with the help of the task managers and you then restart that on both 121 00:10:43,120 --> 00:10:50,230 operating systems, you'll see that now this indeed works and you can hit save here, now what you'll not 122 00:10:50,230 --> 00:10:53,220 see is the output in the log here. 123 00:10:53,230 --> 00:10:58,960 That however makes a lot of sense because if you have a closer look at these save button in our header, 124 00:10:59,780 --> 00:11:06,310 then what I'm doing here in onPress is I'm in the end executing this function but what does this do? 125 00:11:06,370 --> 00:11:11,350 It retrieves our parameters saved in the or stored in the save key and 126 00:11:11,350 --> 00:11:12,570 what is this? 127 00:11:12,580 --> 00:11:17,770 Well that is as mentioned before, a pointer at the save filters function, so we retrieve that pointer. 128 00:11:18,250 --> 00:11:22,630 If we then don't do anything with that pointer, we're not doing anything. 129 00:11:22,630 --> 00:11:27,940 That is a pointer at a function, so of course we need to execute it like this by adding parentheses. 130 00:11:27,940 --> 00:11:34,120 Alternatively we simply get rid of this anonymous arrow function here and we simply bind onPress to 131 00:11:34,120 --> 00:11:40,420 the result of retrieving our pointer here like that because then this pointer at the function will 132 00:11:40,420 --> 00:11:42,270 be executed for us when we press this, 133 00:11:42,280 --> 00:11:43,210 that's the alternative. 134 00:11:43,960 --> 00:11:48,370 So now if we save this and we therefore let this reload and we go back to the filters, 135 00:11:48,370 --> 00:11:55,200 if I hit save here, we now see an output in the log, let's scroll up a bit here, 136 00:11:55,300 --> 00:11:56,940 everything is set to false. 137 00:11:57,040 --> 00:12:02,800 If I now set lactoseFree and vegetarian to true and I hit save, we see another log and there indeed 138 00:12:03,070 --> 00:12:09,080 vegetarian and lactoseFree is set to true which is exactly what I have here. 139 00:12:09,100 --> 00:12:14,420 So now that is working, let's confirm that it also works on Android by going to filters there as well, 140 00:12:14,440 --> 00:12:18,480 let's set lactoseFree to true and save this and if we do so, 141 00:12:18,520 --> 00:12:20,080 that's our Android output here, 142 00:12:20,110 --> 00:12:22,290 lactoseFree is true, everything else is false, 143 00:12:22,330 --> 00:12:24,550 so now this also works. 144 00:12:24,550 --> 00:12:29,830 So this workaround can be a bit tricky to wrap your head around and avoiding this infinite loop here 145 00:12:30,020 --> 00:12:31,270 also is important, 146 00:12:31,330 --> 00:12:38,230 making sure that you will use callback here to avoid unnecessary rebuilds of this function 147 00:12:38,230 --> 00:12:43,850 here also is something you should do, so that when you update the params and therefore this component 148 00:12:43,850 --> 00:12:48,950 rebuilds, you don't also rebuild that function and therefore also enter an infinite loop. 149 00:12:48,950 --> 00:12:54,380 But with that, we have a way of communicating between component and navigation options which is typically 150 00:12:54,380 --> 00:13:00,470 a pattern you need when you have action items in your action bar and you want to trigger something that 151 00:13:00,470 --> 00:13:04,720 depends on data managed in your component with the help of these buttons.