0 1 00:00:00,750 --> 00:00:07,740 In this lesson I'm going to show you how to separate out a React app into even smaller components which 1 2 00:00:07,740 --> 00:00:13,230 are more specialized. And in the process we're going to learn how to use the React developer tools to 2 3 00:00:13,230 --> 00:00:17,610 help debug and manage all of these components. And to begin 3 4 00:00:17,640 --> 00:00:23,140 I've got a copy of the website that we had at the end of the previous cochallenge. 4 5 00:00:23,370 --> 00:00:32,520 So go ahead and fork this starting project and we'll get started by splitting out some of our components. 5 6 00:00:32,520 --> 00:00:39,720 Notice how in our card component, we've got things here which might work better as a single component 6 7 00:00:40,020 --> 00:00:43,170 because in the future we might want to reuse it. 7 8 00:00:43,320 --> 00:00:50,550 And one of the most obvious choices is this avatar image because there might be other parts of our website 8 9 00:00:50,580 --> 00:00:54,660 where we might need a circular image with a white border. 9 10 00:00:54,990 --> 00:01:01,170 So it makes sense to create a custom component and separate it out. In order to do that 10 11 00:01:01,170 --> 00:01:10,740 we're going to extract this image element and I'm going to create a new file called Avatar and then 11 12 00:01:10,800 --> 00:01:16,320 add all of the boilerplate code and I'm going to return this image element. 12 13 00:01:16,320 --> 00:01:22,590 So let's cut it out of here and put it into our avatar function right here. 13 14 00:01:22,610 --> 00:01:31,100 Now if we head back into our card.jsx and we import our avatar component and then replace it 14 15 00:01:31,160 --> 00:01:33,760 where it used to be. 15 16 00:01:33,940 --> 00:01:41,050 Now notice at this point our app breaks because when we copied over that image element it had its source 16 17 00:01:41,140 --> 00:01:48,760 set as the props.image whereas in our new avatar component, we haven't actually yet added the props 17 18 00:01:48,880 --> 00:01:54,500 inputs and we haven't passed any properties to that avatar component. 18 19 00:01:54,640 --> 00:02:01,180 So pause the video and have a think about this and see if you can solve this problem and make the errors 19 20 00:02:01,180 --> 00:02:01,650 go away. 20 21 00:02:03,170 --> 00:02:04,100 I'll give you a hint. 21 22 00:02:04,160 --> 00:02:10,820 You need to think about how to pass over that image property to the newly created avatar component. 22 23 00:02:14,200 --> 00:02:14,530 All right. 23 24 00:02:14,630 --> 00:02:22,610 So in our card component, we know that we get past some properties and these properties come from our 24 25 00:02:22,670 --> 00:02:23,120 App. 25 26 00:02:23,120 --> 00:02:23,930 js 26 27 00:02:24,140 --> 00:02:28,680 and its name, image, telephone and e-mail. 27 28 00:02:28,700 --> 00:02:32,170 Now the image property corresponds to the image URL 28 29 00:02:32,200 --> 00:02:39,380 that's from our contacts.js which is the URL that we want to insert into our avatar image elements 29 30 00:02:39,430 --> 00:02:40,380 source. 30 31 00:02:40,460 --> 00:02:42,830 So we have to pass it down two levels 31 32 00:02:42,830 --> 00:02:43,490 right? 32 33 00:02:43,520 --> 00:02:50,540 So first it's from app to card and then it's from card to Avatar. 33 34 00:02:52,280 --> 00:02:56,960 At this point especially if you have a lot of properties and a lot of components, it can start to get 34 35 00:02:56,960 --> 00:02:58,770 a bit confusing. 35 36 00:02:58,850 --> 00:03:04,370 Now if you really think about it though, we know that we can dig around and see that we've got this prop 36 37 00:03:04,370 --> 00:03:09,480 called image which is being passed almost like an attribute to the card component. 37 38 00:03:09,710 --> 00:03:19,970 And we can pick it up inside here and pass it over as the props.image. And now inside the avatar 38 39 00:03:19,970 --> 00:03:27,620 component, there's now a property called image which we can pick up inside here as long as we add props 39 40 00:03:27,740 --> 00:03:33,670 as an input parameter and we can pick it up inside here and everything's all good again. 40 41 00:03:33,920 --> 00:03:39,170 But there's a much easier way of finding out the names of properties and which components rendered them. 41 42 00:03:40,690 --> 00:03:44,190 If you open up this right sided pane here, 42 43 00:03:44,260 --> 00:03:49,150 if you take a look down here you see a section called the React DevTools. 43 44 00:03:49,150 --> 00:03:55,150 And this might remind you a little bit of our Chrome developer tools and it works in a similar way. 44 45 00:03:55,150 --> 00:04:02,080 So instead of showing the entire HTML DOM tree, it actually shows you your React DOM tree so you can 45 46 00:04:02,080 --> 00:04:08,920 see that we've got an app component at the top of the tree which then split into three card components 46 47 00:04:09,010 --> 00:04:15,150 that we've got on screen and then inside each card component we've got an avatar component. 47 48 00:04:15,280 --> 00:04:20,770 And what's really helpful about these React dev tools is we can peek at the props that are being passed 48 49 00:04:20,770 --> 00:04:23,330 around to each of these components. 49 50 00:04:23,350 --> 00:04:30,190 So for example if I was coding up the card component and I wanted to know which properties I can tap 50 51 00:04:30,190 --> 00:04:36,640 into using props.something I can just click on one of these card components and on the right here 51 52 00:04:36,670 --> 00:04:42,870 I see all of the props that it gets passed and I can see that it's being rendered by the app component. 52 53 00:04:43,630 --> 00:04:49,660 And now that I've got the names and the values of each of these props, I can then go ahead inserting 53 54 00:04:49,660 --> 00:04:53,200 them inside each of my HTML elements. 54 55 00:04:53,230 --> 00:04:59,950 Now I find that code sandbox's React dev tools tends to be quite buggy and sometimes it just doesn't show 55 56 00:04:59,950 --> 00:05:01,330 what it's supposed to. 56 57 00:05:01,330 --> 00:05:08,640 So what I prefer to work on is the actual React dev tools that you can install on Chrome or Firefox. 57 58 00:05:08,770 --> 00:05:15,700 And also if you're working with your local environment so using a local editors such as VS code or 58 59 00:05:15,700 --> 00:05:23,090 Atom, you're going to need this installed anyways. I recommend using Chrome for the entirety of this course. 59 60 00:05:23,110 --> 00:05:27,970 So if you head over to the Chrome web store and search for the React developer tools that's offered 60 61 00:05:27,970 --> 00:05:35,130 by Facebook, you can go ahead and add it to Chrome just by clicking on the add extension button. And once 61 62 00:05:35,130 --> 00:05:39,300 it's done you'll see this little symbol show up next to your search bar. 62 63 00:05:39,300 --> 00:05:45,930 Now if you're using Firefox there's also a React developer tools add-on that you can use and it works 63 64 00:05:45,960 --> 00:05:54,060 exactly the same way. Once you've got this installed then you can actually just simply open up the Chrome 64 65 00:05:54,060 --> 00:06:01,110 developer tools or Firefox developer tools by going to view, developer and developer tools or using the 65 66 00:06:01,110 --> 00:06:03,670 shortcut as you're probably used to by now. 66 67 00:06:03,690 --> 00:06:09,060 So inside here you'll notice right at the end there's a tab called components and it's got this little 67 68 00:06:09,060 --> 00:06:10,630 React symbol next to it. 68 69 00:06:10,710 --> 00:06:18,180 And this is going to be showing our React components on screen. The first time you install the React component, 69 70 00:06:18,180 --> 00:06:19,970 I recommend refreshing the website 70 71 00:06:19,980 --> 00:06:22,940 sSo it actually starts working. And when it does 71 72 00:06:22,980 --> 00:06:29,850 you can see it'll either color itself in red or in blue depending on whether if you're in the developer 72 73 00:06:29,850 --> 00:06:32,520 mode or if you're in the deployment mode. 73 74 00:06:33,000 --> 00:06:40,200 Once this has been activated in our components tab you should be able to see our React component tree. 74 75 00:06:40,230 --> 00:06:43,730 So here you can even use this little select tool. 75 76 00:06:43,890 --> 00:06:49,130 Not this one which is going to select the HTML elements, but this one inside the components tab. 76 77 00:06:49,320 --> 00:06:52,940 And this is going to allow you to select your React components. 77 78 00:06:52,980 --> 00:06:58,890 So for example if I click here it's going to select my avatar components in the tree and you can imagine 78 79 00:06:58,890 --> 00:07:04,230 if you have a very big and complex website with hundreds of components rendered then this can become 79 80 00:07:04,230 --> 00:07:05,460 really helpful. 80 81 00:07:05,610 --> 00:07:11,490 And when I've got my avatar component selected, you can see that it gets a property called img which 81 82 00:07:11,490 --> 00:07:18,830 is the URL of the image. Now if I was to develop my avatar.jsx file, 82 83 00:07:18,950 --> 00:07:25,490 it's quite easy for me to actually go ahead and to know which properties I can tap into when I write 83 84 00:07:25,490 --> 00:07:28,030 props.something. 84 85 00:07:28,070 --> 00:07:32,960 Now a cool thing about this React dev tool is that it works on other websites that you haven't developed 85 86 00:07:32,960 --> 00:07:33,680 as well. 86 87 00:07:33,710 --> 00:07:40,310 For example, if I go to airbnb.com you can see that the color has now changed its become a black 87 88 00:07:40,310 --> 00:07:47,840 and blue and this represents a deployed version of React, so react is in production mode. And if we go 88 89 00:07:47,840 --> 00:07:54,020 ahead and open up our developer tools again and we go to components then we can see that AirBnB is also 89 90 00:07:54,020 --> 00:07:57,550 comprised of a whole bunch of React components. 90 91 00:07:58,040 --> 00:08:04,160 And if we go ahead and use our selector tool maybe we can scroll down and take a look at each of these 91 92 00:08:04,250 --> 00:08:06,540 tile elements that they've got here right? 92 93 00:08:07,010 --> 00:08:10,460 So these seem very much like classic components. 93 94 00:08:10,670 --> 00:08:16,160 And if we click on it you can see they've got a component called E and it's got all of these props like 94 95 00:08:16,220 --> 00:08:22,310 aspect ratio and alt as well as a source URL just like what we have. 95 96 00:08:23,270 --> 00:08:29,450 So the React developer tools makes it a lot easier to debug what's actually going on when your app goes 96 97 00:08:29,450 --> 00:08:29,960 wrong, 97 98 00:08:29,960 --> 00:08:33,240 so when it's not doing something you're expecting it to. 98 99 00:08:33,260 --> 00:08:40,790 For example if inside my avatar component instead of tapping into the props.image instead I thought 99 100 00:08:40,790 --> 00:08:48,080 maybe I had it as an image URL, then I'll notice that in my preview that I don't actually get an image 100 101 00:08:48,080 --> 00:08:55,280 shown. But using my React dev tools I can go ahead and select the image and then take a look at the props 101 102 00:08:55,280 --> 00:08:57,710 that are showing up and I can see, "Oh right. 102 103 00:08:57,710 --> 00:09:03,670 So the URL is actually under a prop called img instead of image URL". 103 104 00:09:03,890 --> 00:09:06,310 So I go ahead and fix that and then bam! 104 105 00:09:06,440 --> 00:09:09,390 Of course my images now start showing up. 105 106 00:09:09,500 --> 00:09:15,290 So we're going to be using the React developer tools to debug our website in the future as well so that 106 107 00:09:15,290 --> 00:09:18,920 when things go wrong, we'll be able to inspect it and know how to fix it. 107 108 00:09:20,220 --> 00:09:27,240 Now if you're wondering where all of the HTML elements are in our tree because if we look at our card 108 109 00:09:27,240 --> 00:09:33,420 component we can see those divs, there's