Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a new line in ReactJS

Introduction

Let's start our journey into the wonderful world of ReactJS, a powerful JavaScript library that allows us to create beautiful, interactive user interfaces. This blog post will focus on a simple yet very important aspect of ReactJS - how to make a new line in ReactJS.

Now, some of you might be wondering, "What does 'making a new line' even mean?" Well, in programming, a new line refers to the act of moving the cursor to the next line, so that whatever we type or display next will appear on a new, separate line. It's like hitting the "Enter" key on your keyboard when you're typing in a Word document.

Why Do We Need a New Line in ReactJS?

Before going into the how, it's essential to understand the why. Why do we need a new line in ReactJS? In any application, there are countless situations where we need to display elements in a new line. For example, we may want to display a list where each item appears on a new line, or we may want to create a form where each input field is in a new line.

How to Create a New Line in ReactJS

Now that we understand the importance of a new line in ReactJS, let's learn how to create it. Here's a simple example where we display two sentences on two separate lines:

const MyComponent = () => (
  <div>
    <p>This is the first sentence.</p>
    <p>This is the second sentence.</p>
  </div>
)

In this example, we're using the HTML p tag to create separate lines. Each p tag creates a new paragraph, which naturally appears on a new line.

Using CSS to Create a New Line

However, what if we don't want to use the p tag? Another way to create a new line is by using CSS. We can use the display property and set its value to block, which will make an element take up the full width of its parent element, thereby pushing the next element to a new line.

Here's an example:

const MyComponent = () => (
  <div>
    <span style=>This is the first sentence.</span>
    <span style=>This is the second sentence.</span>
  </div>
)

In this example, we're using the span tag, which by default does not create a new line. However, by setting its display property to block with inline CSS, we're forcing it to take up the full width of its parent and thus create a new line.

Using JavaScript to Create a New Line

There's also a more JavaScript-y way to create a new line, which is by using the newline character \n. However, since ReactJS does not interpret the newline character as a new line, we have to use the pre tag, which preserves both spaces and line breaks.

Here's how we can do it:

const MyComponent = () => (
  <pre>
    {'This is the first sentence.\nThis is the second sentence.'}
  </pre>
)

In this example, we're using the newline character \n to create a new line, and the pre tag to make sure that the newline character is interpreted correctly.

Conclusion

In this post, we've learned three different ways to create a new line in ReactJS. We can use HTML tags that naturally create a new line like p, use CSS to force an element to take up the full width of its parent, or use the newline character along with the pre tag.

I hope this post has helped you understand how to create a new line in ReactJS. Now it's time for you to practice. Happy coding!