Finding Bugs
Tutorial: Finding Bugs
Overview
In this tutorial, we delve into the importance of identifying and resolving bugs in React development. Bugs are inevitable in any project, but efficient debugging practices can save time and ensure a robust application. This tutorial emphasizes JSX validation and sets the stage for building reusable components, a key topic in this chapter.
Step 1: Understand Reusable Components
Before diving into debugging:
- Reusable Components: These are building blocks in React that can be repurposed across different parts of an application. A reusable component focuses on modularity, ensuring that logic and data are not hard-coded but instead passed as props.
- Key Point: A component containing its model data is inherently less reusable.
Step 2: Debugging JSX Errors
-
Common JSX Errors:
- Unmatched tags or missing closing tags (
div,ul, etc.). - Self-closing tags (e.g.,
<br />) are not properly formatted. - Improper nesting of elements.
- Unmatched tags or missing closing tags (
-
Strategy to Fix Bugs:
- Analyze the Error Message: React's error messages provide detailed information about the problem, including line numbers and the expected structure.
- Isolate Components: Temporarily comment out complex parts of the component to focus on smaller, more manageable sections.
- Validate Structure: Use tools or linters like ESLint to ensure your JSX follows best practices.
Step 3: Example Walkthrough - Debugging a Footer Component
- Problem: The
footercomponent contained an unmatched<ul>tag, causing an error. - Steps Taken:
- Checked React's error message to locate the problematic line.
- Verified the nesting and self-closing of tags (
<ul>was incorrectly self-closed). - Corrected the issue by ensuring proper nesting and structure.
Step 4: Preparing for Reusability
-
Extract Model Data:
- Move static data (e.g., links, titles) into a separate
modelordatafile. - Example:
const footerModel = [ { title: "Location", content: "Some address here." }, { title: "Around the Web", content: [ { key: "facebook", url: "http://facebook.com", className: "fa-facebook" }, { key: "twitter", url: "http://twitter.com", className: "fa-twitter" }, ], }, { title: "About Freelancer", content: "Freelancer is a free Bootstrap theme." }, ]; - This ensures the
footercomponent can dynamically consume this data without hardcoding.
- Move static data (e.g., links, titles) into a separate
-
Dynamic Rendering:
- Iterate over the model data to generate JSX dynamically.
- Example:
footerModel.map((section) => ( <div key={section.title}> <h3>{section.title}</h3> <p>{section.content}</p> </div> ));
Step 5: Key Takeaways
- Debugging is a systematic process. Always rely on error messages and isolate components to resolve issues efficiently.
- Reusability begins with separating data (model) from the view (components).
- Use arrays and objects to structure dynamic content, enabling flexible and reusable components.
What’s Next?
In the next tutorial, Extracting the Model from the View, we will further separate concerns by moving static data into its own module, making components leaner and more reusable.
Video Transcript
00:00Welcome to a new section that is completely dedicated
00:03to building reusable components.
00:06Through this section, we're gonna learn more about
00:08extracting data, making things dynamic, validating content,
00:12setting default values, and much, much more.
00:16In this first lecture, what we're gonna do is we're gonna
00:18focus a bit on bugs, which happened to me
00:21and every other developer,
00:23and how you actually deal with it in the most efficient way.
00:26So we're gonna talk about validating your JSX,
00:29and we're gonna talk about in general, how do you deal
00:31with errors in react?
00:34Alrighty, let's continue. So we really just have a few more
00:37sections left to create, really starting with the footer.
00:40So I already went ahead and selected the footer.
00:42Go ahead and do the same.
00:44And I'm gonna copy that out
00:46and cut that in out more accurately and create a new file.
00:49We're gonna call it footer js.
00:52Save it inside of our components folder.
00:54And let's go ahead and paste that in.
00:56And just as always, I'm gonna go ahead
00:58and change our class references by searching.
01:02First, let's word wrap.
01:03And then I'm gonna search
01:04and replace every instance of class with class
01:09name replace.
01:11All. Beautiful. Now, next, again, as always,
01:14I'm gonna double check
01:15and confirm that everything here is self enclosed
01:19and it looks quite good actually.
01:21Just that one HR that I had to change. Beautiful.
01:24So let's copy that, save that. Oh, you know what?
01:26Let's again go to one of our components.
01:30How about will go to our header?
01:31And I'm just gonna copy everything here.
01:35Go to our footer
01:37and set our return,
01:42close our render, close our class, grab our footer
01:47and paste it right in.
01:48Already. This is almost automated,
01:49this process that's so easy.
01:51Now, the one thing that isn't easy is really, well,
01:54not necessarily not easy, is your goal is to create classes
01:56that a lot are a lot deeper than just the classes
01:59we've created right now.
02:03What we wanna do is basically we wanna create
02:06as many components as we can
02:07so we could create reusable components, which is really part
02:10of the main topic of this lecture.
02:13Now, when we're talking about reusable components, let's,
02:16first of all, if we have all of our model data inside
02:19of our component, well, by nature,
02:22our component is not reusable.
02:24True, true. So for us to create a reusable component,
02:28and even let's say that, uh,
02:30this element here is a reusable,
02:32but really we would create probably even every content in
02:35this li as a reusable item,
02:37create another component around it.
02:39But for now, just for the sake
02:41and purpose of our presentation, you could say, what I want
02:44to do is I'm gonna create here just quickly a module
02:47and let's, let's make it as a let model
02:50and let's just try to create the data structure
02:52that is in existence inside of here.
02:55So first of all, when we're looking at it,
02:57we have a container and it has a, oh, you know what?
02:59Let's, let's even do something better.
03:01Let's take a deep look at our footer visually
03:03and see what is the construct of that footer.
03:05So let's go ahead and create that footer. I'm gonna save it.
03:09Go into our, uh, la la la client folder
03:13and in our client, I'm gonna go ahead
03:15and load up our footer from
03:20footer and let's just double check
03:23that our footer is called footer
03:25and it's exporting as the default.
03:26Beautiful. All is good. So let's go.
03:29And instead of rendering all the dispute,
03:31I'm just gonna go ahead, take it outside, comment it out
03:36'cause we're gonna need it a little bit later.
03:38And let's, instead of that, send in our footer,
03:41just our beautiful footer
03:43so we could take a deeper look at it when we're looking at
03:45it on our browser.
03:47So first of all, we have your small error.
03:48So let's see, what is that error? It must be a synex.
03:51So we're using B in line around line 13.
03:54So let's search for that br around line 13. Here we go.
04:00And let's give it another shot.
04:03And we still have another one.
04:04So let's see, what's the next one in our error here, parse,
04:09da da da.
04:14And we have your diviv. Without closing it, it seems okay.
04:18So let's try to locate it around line 33.
04:22It's even telling us where it's expecting a close
04:25or when it's a over close.
04:26So after our ui.
04:27So let's take a look to see if there's something missing
04:30after that u uh, after that ul.
04:33So we're closing a diviv,
04:34but let's see, where is that opening div?
04:38And that opening div is right here.
04:40And that looks good to me.
04:42And we have your diviv and it's closed as well.
04:46Let's see what the next one. So the next one we have a
04:47opening, we have a closing,
04:49and then what's the next one above?
04:52That's a whole row. Okay, then we have a container.
04:55So we need another two. We got that.
04:59Let's take a deeper look at that error.
05:00'cause the error seems to be around here.
05:02Maybe it's not even an error in that diviv,
05:05maybe the error itself is not explicit enough.
05:07But let's, let's take a deeper look into this error.
05:11So we have here model build, failed,
05:17expected, corresponding JSE closing for how interesting.
05:21So it's not finding our closing tag, which probably means
05:24that we're closing something that not that well in line.
05:27If it's looking for it in line 34, uh, that means it,
05:32it's, it was expecting to close it right here.
05:35That is so weird.
05:38Oh, our UL is self enclosed. There you go.
05:42Did I just do that? I have no idea.
05:44But you know, that was a really hard bug to catch.
05:46And literally what we did was we tried to isolate
05:49to find our diviv.
05:51We found our diviv and then we went up
05:53to try to find our clothes.
05:55Not that easy. I'm not gonna tell you it's easy,
05:56but we found it.
05:58Let's give it another round. And here we go.
05:59Our, our element is rendering out.
06:01Now, before we wrap up this lecture, um, let's go ahead
06:05and extract our data so we know we have your three points
06:08of entry that are in the same level of priority.
06:11So let's go ahead and create three separate, uh,
06:15array elements you could say.
06:16And inside of here, let's go ahead
06:18and inside of our model that we're gonna be creating, uh,
06:22which we're gonna extract in a minute, let,
06:25and we'll make it as an array.
06:27And each array and the array will have three
06:30objects as children.
06:35And let's see if we have share, share commonalities.
06:37So commonalities are titles.
06:39So let's go ahead and set here a title.
06:41Now, a lot of times what will happen in real world
06:44applications is that in the real world,
06:47your data will define how your structure
06:49of the content will be.
06:50In our case, we're lucky 'cause we could look at the data
06:53and through that data decide how we wanna structure it.
06:56So in our case, I could see that here we have text here,
06:59we have text or maybe HTML style text.
07:02So what I could do is I'm gonna go ahead here.
07:05So I'll put here just content.
07:07I'll make it agnostic so it doesn't really know
07:09what the content is.
07:14Beautiful. Last but not least, content.
07:18And let's go ahead and start putting that content in.
07:20So we have location and we have
07:25around the web
07:30and we have about freelancer.
07:35Beautiful. Alright.
07:39Now we have also our content.
07:40And as we said in our content,
07:42we have two different types of content.
07:43We have a string based content supposedly,
07:46and we have an HTM,
07:47and we have a, you could see a deeper level type of content.
07:51So let's start with the easier stuff.
07:52I'm just gonna copy this content, this plain string.
07:56And I'm gonna go ahead and I'm gonna oto copy.
07:58And by the way, this, I can't break lines in regular text,
08:02so I'm gonna have to or use special quotes,
08:04or I could literally just put that b
08:06that was in the early in there earlier that I'm fine with
08:09that 'cause it's just a string.
08:11And I'm gonna go ahead and note it
08:12for this freelancer bootstrap element.
08:15And I'm gonna go ahead and put that content in.
08:17And imagine that, let's say we put the link in here as well.
08:19So this could be any HTML,
08:21while this would probably be an array, an array of links.
08:25And if we take a deeper look into, uh, these structures,
08:28we'll see that we have links,
08:29but they're not really established.
08:30And then the only difference between each item,
08:33they're all social elements,
08:35but the only difference between those elements are that fa,
08:38Facebook, fa, Google plus fa, Twitter, fa,
08:41LinkedIn, fa dribble.
08:43So, uh, what, what I'm gonna do is, uh, again,
08:46I'm gonna create here an array.
08:48And in this array let's just put here basically maybe a,
08:51a key and well, let's go ahead
08:54and put this like, like a, like an object.
08:57And one will be a key
08:59or really the name here is, you know, let's do one as a URL.
09:03And in our case, they're all really the same URL currently.
09:06But let's set that up. And the next will be, um, blah
09:10class name.
09:13How about class name? Although class name, no,
09:15let's just give it a key.
09:16Uh, that was a good choice in the beginning.
09:19Okay, so now what I'm gonna do is I'm gonna go to each
09:21of these FA Facebook,
09:22I'm just gonna take those unique stuff.
09:25So I'm gonna do that right now and we're gonna speed it.
09:29So you're gonna see me doing it really quickly.
09:30And in the next lecture we're going to continue
09:33through the process of actually, um, weaving this,
09:37this information, this module.
09:39First we're gonna extract it into our client
09:41and then we're gonna weave that information into our class.
09:45In this lecture, our focus was errors,
09:47but it really leads us to our main topic,
09:50which is reusability.
09:52In the next lecture, we're gonna move our attention into
09:55extracting our model data from the view.
