React - Next Blog: Issue with empty lines displaying on the browser

Currently, I am developing a blog that retrieves data from Mongo Atlas. The blog is built using Next.js version 9.5.4 and is hosted on Vercel. The blog page utilizes static props and regeneration.

The data is fetched in JSON format and then stringified for processing.

 return {
      props: {
        articles: JSON.parse(JSON.stringify(articles)),
      },

Each blog post (referred to as articles) consists of a title, text, and a signature line with the author's name and publication date. Some articles include links while others feature one or two pictures. Here is the relevant portion of the code:

{articles.map((article) => (
      <li key={article.id}>
      <h2  className={styles.blogtitle} >{article.title}</h2>
      <p  className={styles.blogtext}>{article.text}</p>
      <img src={article.image1} className={styles.blogimage} alt={article.alt1} ></img>
      <img src={article.image2} className={styles.blogimage} alt={article.alt2} ></img>
      <p>{""}
      <a
       href={article.link} 
      title={article.link}
      target="_blank"
      rel="noopener noreferrer" >
      {article.link}            
      </a>
      </p>
      <p  className={styles.bloginfos}>{article.author}, today at {article.date.slice(0,10)} - {article.date.slice(11,16)} h</p>
      </li>
    ))}

After adding images and links, posts without corresponding content display empty lines for two images and one link. Upon inspection with the browser console, it appears that each list item receives:

<img class="nextGeneratedName1"> <img class="nextGeneratedName1">

however, these elements are empty. Additionally, there is a p tag containing:

<a target="_blank" rel="noopener noreferrer"></a>

This behavior causes undesired empty space in the displayed content. It seems that Next.js automatically adds the image classes even when no content is present. The empty paragraph should not be rendered in HTML since it lacks both content and a specific class. For further reference, the entire project is available on GitHub. You can view the blog.js file here.

Answer №1

To ensure that the image url is displayed only when it is available, you can perform a condition check. For example, in line 30 of your blog.js file, you can do the following:

{article.image1 && <img src={article.image1} className={styles.blogimage} alt={article.alt1}></img>}

You can apply similar logic for other elements like a, p, etc., that may potentially be empty under certain circumstances.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arrange the elements within the anchor tag in a vertical line using Angular Material

Is there a way to style the elements within an anchor tag in Angular Material so that they display in a single line? I am looking for the CSS solution to achieve this outcome. This is my current HTML code: <a mat-list-item [routerLink]="['das ...

What are the steps to enable CSS code completion for JavaFX again?

Hello everyone, Recently, I disabled the CSS code completion feature for JavaFX-Tags such as '-fx-...'. Unfortunately, I'm not sure how or why it happened but now I would like to enable the code suggestions again. I have been unable to loca ...

Creating an enum from an associative array for restructuring conditions

Hey everyone, I have a situation where my current condition is working fine, but now I need to convert it into an enum. Unfortunately, the enum doesn't seem to work with my existing condition as mentioned by the team lead. Currently, my condition loo ...

Does the <Query> element handle caching automatically as well?

While using the Query component from the Apollo client, I noticed that after successfully retrieving data from the server and checking the Apollo devtools console, the cache appeared to be empty. https://i.sstatic.net/JzRbP.png When following the fullsta ...

Showing different HTML elements based on the link that is clicked

Recently, I delved into the world of web development and decided to test my skills by creating a basic webpage with an interactive top navigation bar. Depending on the link clicked, specific HTML elements would be displayed while turning off others using a ...

Arranging the elements in my footer for optimal display

My footer layout expands and the lists in each column become misaligned as I add more links to it. I want to make sure that the lists for each column are aligned horizontally on the same row, with the title of each column also in the same row. levi | ...

Combining Tailwind with Color Schemes for Stylish Text and Text Shadow Effects

tl;dr I have a utility class in my tailwind.config.ts for customizing text shadows' dimensions and colors. However, when using Tailwind Merge, I face conflicts between text-shadow-{size/color} and text-{color}. The Issue In CSS, text shadows are oft ...

The error message indicates that the 'aboutData' property is not found within the 'never[]' data type

What is the correct method for printing array elements without encountering the error message "Property 'post_title' does not exist on type 'never[]'?" How can interfaces be used to define variables and utilize them in code for both ab ...

Deactivate the dropdown menu without triggering any event

I am working on a JSP page and I need to find a way to disable a dropdown list without using an onclick method or event in the dropdown menu itself. <% boolean condition = true; if(condition){ // Here, I need to access 'plc1' ...

Having trouble downloading a PDF file on a local server with React and the anchor tag element

Having trouble downloading a pdf file from my react app to my Desktop. I've reached out for help with the details How to download pdf file with React. Received an answer, but still struggling to implement it. If anyone new could provide insight, that ...

Tips for removing a Material UI chip component using the Material UI GitHub label selector demonstration

I recently experimented with the autocomplete feature of Material UI using the github label picker example. I made some modifications and you can check out the sandbox below: https://codesandbox.io/s/material-demo-hwi3l?file=/demo.js The Autocomplete func ...

Is there a way to position these divs in a line using inline-block?

I need help aligning two divs with text differently - one to the top left and the other centered but not on the same line. Here's the layout I'm aiming for: ---------------------------------------------- |Backstage |Issue 4 | | ...

The type 'MouseEvent<HTMLButtonElement, MouseEvent>' cannot be matched with the type 'boolean'

Just starting out with TS and running into a problem that TS is pointing out to me. Error: Type '(x: boolean) => void' is not compatible with type '(e: MouseEvent<HTMLButtonElement, MouseEvent>) => void'. Parameters ' ...

What could be causing the Express error when this.props.param.match.id is coming up as undefined?

I am currently delving into the world of Express.js, attempting to understand how to utilize it effectively. However, I am encountering an issue where I am unable to retrieve an id value from this.props.match.params. Here is how my express route is set up ...

Displaying a custom error page in Next.js with the appropriate status code

I recently implemented a custom error page following the instructions provided in the documentation. My goal was to use this error page for specific errors that may occur during the getStaticProps function. Here's how I structured it: const Page: Next ...

I am encountering an error when trying to run the command npm run dev. How can I resolve

I utilized vite to create a frontend template. The package.json resides in the frontend directory and includes development tools, but I'm encountering an error. This project involves Django and React. I am using a Python virtual environment, and the b ...

Restoring list item to standard CSS styling

I am having trouble with the alignment of my links. They are currently displayed in a horizontal manner instead of vertically like this: First link Second link Third link The current layout shows the links next to each other as follows: Firs ...

Only a select few expandable elements in the jQuery accordion

How can I create an accordion menu with only a few expandable options? I am looking to include the following items in my menu: Home, Support, Sales, Other The "Home" option is just a link without any sub-menu. Clicking on it should direct users to a spec ...

The strategy for synchronizing API calls across multiple tabs in a browser

Can an API be called on multiple browser tabs simultaneously? For instance, in Tab A I make a call to the apple API with data A, and in Tab B I also call the same apple API but with data B. This means that the apple API in Tab A should ...

Can someone provide guidance on utilizing parentheses in HTML code?

I am facing an issue in my Angular project while using the WordPress API. The problem lies in the API address structure which includes "" like this: <img src="{{importantvideo.better_featured_image.media_details.sizes.["covernews-med ...