Starting Fullstackopen Part 5

Today, I started Fullstackopen part 5, which deals with React & frontend authentication. It's been a while since I coded in React so I had some issues getting my code to work.

One issue I was dealing with was conditionally rendering blogs if they were present. For some reason, my react code was fetching blogs from the API, but I was getting errors with blogs being undefined.

Here was the original code where I was checking if blogs were present or not:

if(!blogs) { return ( <div> No blogs present </div> ) }

Long story short, after 2 hours of debugging and trying out different fixes, I realized that empty arrays are not considered to be falsy! The blogs in my code was an array of blog objects, and I was checking to see if it existed based on an if statement. However, I assumed that an empty array would be evaluated to false, but this is not the case. To fix the bug, I modified my if statement to check the length of the array:

if(blogs.length === 0) { return ( <div> No blogs present </div> ) }

This worked and one coding obstacle was finally cleared!