What is the method for inserting a line break into a string that is being transferred to a component in Next JS?

I am currently collaborating on a group project that involves developing a website. One of my team members created a ContentBlock component to facilitate the creation of new pages (see component code below). I have been working on a new page for the site and attempting to pass a string with newline characters to the component, but they are not displaying correctly.

export const ContentBlock = ({ title, content }: { title: string,  content: string }) => {
  
return (
    <div style={{ width: '100%', paddingTop: '20px', paddingRight: '10px'}}>
      
      {/* Block Header */}
      <h2 style={{
        borderBottom: '4px solid #A1DAFD',
        borderTop: '4px solid #A1DAFD',
        textAlign: 'left',
        padding: '10px ', color: '#4434A6', fontSize: '40px', fontFamily: 'Sanchez', fontWeight: 'bold' }}> {title} </h2>
    
      {/* Block Content */}
      <p style= {{color: '#4434A6', fontSize: '24px', fontFamily: 'League Spartan', paddingTop: '15px'}}> {content} </p>
    </div>
  );
}

I have attempted to use the newline character within the string passed to the component as well as trying the
tag, but both methods only display the characters without creating new lines.

<div style={{display: 'flex', flexDirection: 'column', alignSelf: 'stretch'}}>
                    <ContentBlock title="Sample Title" 
                        content="Sample Content 1\n
                        Sample Content 2\n
                        Sample Content 3\n
                        Sample Content 4"></ContentBlock>
                    <ContentBlock title="Second Content Block" content="Example"></ContentBlock>
                </div>

Answer №1

The "ContentBlock" component utilizes the CSS property "white-space" set to "pre-line" for the paragraph element, allowing for proper rendering of newline characters.

export const ContentBlock = ({ title, content }: { title: string,  content: string }) => {
  
return (
    <div style={{ width: '100%', paddingTop: '20px', paddingRight: '10px'}}>
      
      {/* Block Header */}
      <h2 style={{
        borderBottom: '4px solid #A1DAFD',
        borderTop: '4px solid #A1DAFD',
        textAlign: 'left',
        padding: '10px ', color: '#4434A6', fontSize: '40px', fontFamily: 'Sanchez', fontWeight: 'bold' }}> {title} </h2>
    
      {/* Block Content */}
      <p style= {{
      color: '#4434A6',
      fontSize: '24px', 
      fontFamily: 'League Spartan',
      paddingTop: '15px',
      white-space:    'pre-line' // Renderz newline character as line breaks
      }}> {content} </p>
    </div>
  );
}

This method effectively renders newline characters as line breaks within the content block.

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

reveal the concealed divs within the list elements

html: In my HTML document, I have a unordered list (ul) with each list item (li) constructed like this: <li class="A">list-item <div>1</div> <div class="B">2 <div class="C">3</div> </div> ...

Having trouble understanding why getStaticProps function is not loading before the main exported function

When I use npm run dev to troubleshoot this issue, it utilizes getStaticProps to process various d3 properties before injecting them into the main output function during runtime. However, it seems that getStaticProps is not running as expected - a consol ...

Changing the content of the initial post in a loop on WordPress

<?php $freeadvice=new WP_Query('category_name=freeadvice&showposts=10'); while($freeadvice->have_posts() ): $freeadvice->the_post(); ?> <li class="event"> <input type="radio" name="tl-group" /> ...

Dynamically insert innerHTML content into table rows using JavaScript in PHP is a fantastic way to customize

Having trouble with innerHTML in this scenario, looking to achieve something along these lines: <table width="100%" border="0" id="table2"> <?php include("connection.php"); $sql=mysql_query("select* from b1"); while($res=mys ...

JS custom scrollbar thumb size issues in relation to the scroll width of the element

I recently implemented a custom scrollbar for a component on my website. To determine the length of the scrollbar thumb, I use the formula viewportWidth / element.scrollWidth;. This provides me with a percentage value that I then apply to the thumb elemen ...

Creating an AngularJS directive specifically for a certain <div> tag

Recently, I began learning angularjs and came across a script to change the font size. However, this script ended up changing all <p> tags on the entire webpage. Is there a way to modify the font size of <p> tags only within the <div class=" ...

Is it possible for me to transform this code into a useful helper function?

I am looking to optimize this conditional code by converting it into a helper function using a switch statement instead of multiple if statements. How can I achieve this in a separate file and then import it back into my component seamlessly? import { ...

When attempting to click on the edit button, an Unhandled Runtime Error of TypeError occurs stating that the

Encountered an issue while attempting to click on the "Edit user info" button on my admin page. Upon clicking it, I was presented with this error message: https://i.stack.imgur.com/54cZy.png. The problem appears to originate from my UserForm.js: 'use ...

What is the best way to obtain a user's ID on the server side?

I'm currently working on a node.js application using express and I am in need of retrieving the user ID. I would like to have something similar to "req.userID" so that I can use it in the following way: var counter=0; var user = new Array(); router.g ...

When consecutive DOM elements are hidden, a message saying "Hiding N elements" will be displayed

Provided a set of elements (number unknown) where some elements should remain hidden: <div id="root"> <div> 1</div> <div class="hide"> 2</div> <div class="hide"> 3</div> <div class="hide"&g ...

The property cannot be set because it is undefined in nodejs

var list = [ { title : '', author : '', content : '', } ] router.get('/japan',function(req,res){ var sql = 'select * from japan'; conn.query(sql,function(err,rows,fields){ for(var i = 0 ; ...

Issue: "https://next-auth.js.org/errors#adapter_error_getsessionanduser No metadata available for entity "SessionEntity""

Since upgrading to NextJS 13.5 from 13.4, I have been encountering an error every time I attempt to log in. My setup involves next-auth with TypeORM-Adapter connected to PostgreSQL. The strange thing is that this error only occurs in production mode and no ...

$filter is functioning correctly, however it is generating an error message stating: "Error: 10 $digest() iterations reached. Aborting!"

Here is an example of a JSON object that I am working with: { "conversations":[ { "_id": "55f1595d72b67ea90d008", "topic_id": 30, "topic": "First Conversation", "admin": "<a href="/cdn-cgi/l/e ...

Problem with Marionette AppRouter compatibility when used with Browserify

app.js module.exports = function () { if (!window.__medicineManager) { var Backbone = require('backbone'); var Marionette = require('backbone.marionette'); var MedicineManager = new Marionette.Application(); Medicin ...

Implement a feature in JavaScript that highlights the current menu item

I'm currently developing a website at and have implemented a JavaScript feature to highlight the current menu item with an arrow. The issue I'm facing is that when users scroll through the page using the scrollbar instead of clicking on the men ...

"Transforming an array using the map method to generate an object containing arrays optimized for

I'm looking to extract an array of objects from a nested array within JS/React. I attempted the following approach, but it resulted in an error regarding react children - indicating that objects cannot be rendered as children and suggesting the use of ...

The functionality of the WordPress Contact Form 7 Plugin becomes erratic when integrated into dynamically loaded AJAX content

I am currently facing a challenge with integrating the WordPress Contact Form 7 Plugin into a website I have built on WordPress. The theme of the site utilizes jQuery to override default link behavior and AJAX to load pages without refreshing the entire pa ...

Be cautious when combining formik-material-ui TextField and material-ui TextField in the same component as it may result in a warning about the prop `className` not matching

There is a warning that appears when utilizing both formik-material-ui TextField and the original material-ui TextField (which is connected to Formik with the fieldToTextField function) in a single component. The className prop does not match. Server: "P ...

Exploring the best practices for organizing logic within Node.js Express routes

I'm currently utilizing the https://github.com/diegohaz/rest/ boilerplate, but I am unsure about the best practice for implementing logic such as QR code generation and additional validation. My initial thought was to include validation and password ...

Google Interview Challenge: Finding Pairs that Sum Up

My approach to solving this problem involved iterating through the array and checking if there exists an item such that the sum equals array[i] + item. If such a pair is found, I return true; otherwise, I return false. Now, my main inquiry is: How can I m ...