Showing posts with label React. Show all posts
Showing posts with label React. Show all posts

Thursday, January 6, 2022

The Art of Debugging

I mentioned in a previous post that I am in the process of creating a prototype web application using React. Unfortunately I am also learning how to debug my React code. I created a base version of the application using the tutorial I mentioned in my previous post. Once I got all of that working, I started expanding.

React takes the code you write and compiles the code so that it runs quickly. This compile step is where I find a lot of errors. I have a console window open that spits out a bunch of important information each time I save a file using my interactive development environment (IDE). Sometimes I will get errors and other times I get warnings. While the code is supposed to run with warnings, it generally means something is wrong and is best to fix it. Otherwise your application may not behave exactly as you thought it should.

The worst problems I have encountered are runtime errors. That means my code compiles just fine but the application doesn't look right or doesn't render at all. The first such problem that took me a while to solve involved the HTML break tag. I had a picture and wanted to have a caption underneath it. To make everything look clean, I laid out everything in a table. To get the layout perfect, I used a test HTML page and then just transferred everything to React when I was happy. I didn't know that React treats the break tag a little different and required a slash in the tag. Instead of using <br>, I had to use <br />. That looks pretty simple, right? Well it broke the whole application. Instead of seeing a funny-looking table, I didn't see anything. In order to find the offending tag, I had to copy each part of the HTML table into React and discover when things stopped working.

Today I ran into another problem. I have a button component that looks very integrated with the entire site. It is something I copied from the tutorial mentioned earlier. The guy giving the tutorial didn't bother to do anything special with the button other than to have it jump to a single page. I have put a number of those buttons all over the place but no matter which button you click on, it goes to the same page. Today I wanted to connect one of the buttons to a different page. I looked through the code and figured out a way to make it happen. Unfortunately when I added the code, it broke everything. Not a single page would load. It turns out that by adding the destination page as a parameter to the button, I broke every instance of that button. Seeing as I had a button on the heading of every page and on the footer, that should have been a sign to add the destination page as a parameter to all of the buttons in the application. Once I did that, everything started working again.

The Internet has been very helpful in solving a number of problems. I tried to embed a YouTube video into my application only to get an error from my browser saying that it can't show the video as a security precaution. A quick search showed that I needed to add a library to React and then things started working great.

I'm sure there will be many more problems I run across, but so far I have been able to solve them in a timely fashion. If I feel I am spending too much time on an issue, I put it on hold and work on something else. Sometimes I will go for a walk and the solution will come to me. Ultimately there are a number of tricks to debugging computer programs and it doesn't matter if you are coding in Python or React. I suppose the most important trick is to not get frustrated. I'm still working on that one.

Tuesday, December 21, 2021

Learning React

If you ever find yourself looking for a new job, you will soon discover that you need something other than job hunting to keep you busy. I have started a project to build a graphically pleasing website that is also database driven. I am amazed at how much happier I am now that I have results I can see from my day's efforts. I also spend an hour or two looking for the perfect job, but it isn't all encompassing like it has been.

Right now I am focusing on building the front end or the user experience (UX) part of the website. I did a bunch of research and discovered that all of the cool kids are using React. I found a great tutorial on YouTube that is forming the basis of what I want to build. Of course there are a number of other graphical elements I want to add that are not covered, but that is what Internet searches are for. Those searches also help with figuring out why my version of the project isn't working. The tutorial I found was created over a year ago and the React libraries have changed since then. When something doesn't work, I just have to query the Internet and I have a fix.

Last night I worked on the project for hours and didn't notice the time passing. This morning I awoke early and eager to continue my journey of learning. This is in sharp contrast to the depression I felt after getting one more rejection from a job where I expected to receive an offer. With the general shutting down that is happening at companies due to the Christmas break, I am glad I have something that energizes me and keeps me excited. At the end of the break, I hope to have a fully functioning demo that I can show.