Is there a way to have line breaks done automatically within the <pre> tag?

Struggling to insert a code snippet into my React application, it's odd that no matter what I try, the format doesn't include line breaks. Below is the code snippet:

import React from 'react';
import './Hello.scss';

const Hello = () => {
    return (
        <>
            <div>
                Hello world!
            </div>
            <pre>
                npx create-react-app my-app
                cd my-app
                npm start
            </pre>
        </>
    )
}

export default Hello;

pre {
    font-size: 16px;
    overflow: auto;
    padding: 10px;
    background-color: #f8f8f8;
    border: 1px solid #ddd;
    white-space: pre-wrap; /* css-3 */
    white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */
}

Expected content inside the pre tag should display as:

npx create-react-app my-app
cd my-app
npm start

However, currently displaying as:

npx create-react-app my-app cd my-app npm start

Any suggestions on how to resolve this issue with pre?

Answer №1

To ensure proper line breaks in your code, you can either include the newline character `\n` at the end of each line or use the string as children within your elements.

const Hello = () => {
    return (
        <React.Fragment>
            <div>
                Hello world!
            </div>
            <pre children={`npx create-react-app my-app
cd my-app
npm start
`} />
<pre>
npx create-react-app my-app{'\n'}
cd my-app{'\n'}
npm start{'\n'}
</pre>
        </React.Fragment>
    )
}

ReactDOM.render(<Hello />, document.getElementById('hello'))
pre {
  font-size: 16px;
  overflow: auto;
  padding: 10px;
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  white-space: pre-wrap;
  /* css-3 */
  white-space: -moz-pre-wrap;
  /* Mozilla, since 1999 */
  white-space: -pre-wrap;
  /* Opera 4-6 */
  white-space: -o-pre-wrap;
  /* Opera 7 */
  word-wrap: break-word;
  /* Internet Explorer 5.5+ */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="hello"></div>

Answer №2

When using the <pre> tag along with the <br> tag, you can easily create line breaks within your HTML code.

To achieve this, simply replace the \n symbols with <br /> tags like so:

<pre>
    npx create-react-app my-app<br />
    cd my-app<br />
    npm start<br />
</pre>

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

React Redux failing to correctly map the action to props

I have a function called updateChapter that is responsible for updating the database. I have included this function in my component named EditChapter. However, when I checked the props of the component, the updateChapter action was missing. In another com ...

Bootstrap table exceeds the boundaries of the smartphone screen

I'm currently working on my first laravel project with bootstrap (https://getbootstrap.com/) and I've encountered an issue related to mobile device responsiveness. The problem arises when using a basic table in bootstrap4 (the absence of the res ...

Building an Angular form that is reactive and dynamically populates fields from an array

I am currently working with Angular 9 and I am facing a challenge in implementing a component that utilizes reactive forms. Below is a snippet of my code: approval-edit.component.ts public nominationAllOf: NominationAllOf[]; public approvalEditForm: Form ...

Problem with the purity of :global() CSS-module selectors in NextJS

Currently in the process of migrating an app from CRA to NextJS, I've encountered an issue with the .module.scss files of certain components and pages: Syntax error: Selector ":global(.label-primary)" is not pure (pure selectors must contain ...

Is there a way to conceal the scrollbar in the mobile view (on a mobile device or emulator) in a React application without disrupting the scrolling functionality?

I am looking to conceal the scrollbar on mobile devices throughout my app while still allowing users to scroll. I have attempted to hide the scrollbar using CSS properties like scrollbar, scrollbar-thumb, scrollbar-thumb:hover, and scrollbar-track. This ...

Sending data to another page in React Native can be achieved by passing the values as parameters

I am currently working on passing values from one page to another using navigation. I have attempted the following code: this.props.navigation.navigate('welcome', {JSON_ListView_Clicked_Item:this.state.email,})) in the parent class where I am s ...

Arranging rows and columns in Bootstrap 3 for small screens

My layout is structured as follows: <div class="container"> <div class="row"> <div class="col-md-8> <div class="row"> <div class="col-md-12"> box 1 & ...

Displaying real-time server responses using jQuery, PHP, and AJAX

Is there a way to display server response in real-time as it is being sent during a long and time-consuming request? Scenario: 1) A JQuery function sends an AJAX request to the server and opens a Dialog widget to wait for the response to update the conte ...

Runs tasks asynchronously in a sequential manner

I am attempting to run two functions asynchronously in series using node.JS, but I am struggling with the implementation. Currently, my code looks like this: Function 1: function search(client_id, callback) { clientRedis.keys('director:*', ...

Issues with image sizing on mobile devices

After finalizing my header design, I encountered an issue with the mobile version of the website. The images in the header are not responsive and do not adapt well to different screen sizes. I need assistance converting the header design into functional co ...

Organizing express queries independently of routes

I am currently working on creating an express route that allows me to input an equity name as a query parameter by using ?symbol= in the URL. Once the equity name is provided, I intend to include a new route after that. const express = require("expres ...

What methods can a web server or website use to trigger the re-caching of its own content?

We have recently launched a simple HTML and JS website that requires frequent updates to be reflected to users quickly. However, we are facing challenges with users having to clear their caches. We are exploring options within the HTML file or the Apache w ...

Angular does not employ any Bootstrap styles when rendering

I have successfully installed both Ngb and Bootstrap 4 using the commands provided below, with the root module importing the package as well. npm install @ng-bootstrap/ng-bootstrap --save npm install bootstrap@next --save Within my code, I am attem ...

Trouble aligning a div at the center within another div using margin auto

I am facing an issue with centering a div horizontally inside another div. Here is my HTML code: .block { padding: 0px 20px; max-width: 820px; margin: 0 auto; background-color: #ff0; } .container { margin: 0 auto; display: inline-block; bac ...

The global jsx style does not appear to be taking effect

My custom _document file contains the following code. Strangely, the styles defined within <style global jsx /> do not appear on initial render. During development, the styles are applied only when I refresh the page, but after building my site for ...

Issue with mime-type when uploading a file in a React js application on Windows operating system

In my React application, I have implemented the following HTML code to upload files: <div className="form-group files"> <label>Upload Your File </label> <input type="file" className="form-control" multiple onChange={this.onC ...

Issue with jqGrid not showing JSON data received from Java application

My Java Service Implementation: @RequestMapping(value="/refreshInterviewServiceList1", method = RequestMethod.GET) public @ResponseBody JSONObject refreshInterviewServiceList1() throws JSONException{ List<ReportInterview> reportInterview = repo ...

"Embedding PHP code within HTML tags, which is then embedded within

Running into an issue within a while loop... echo 'var contentString = '<div id="content" > <div id="bodyContent"> <p>' + $row[name]+ '</p> ...

Troubleshooting a Typescript issue with "padStart" while attempting to add an object to an array

In my code, I define an object template with empty values for publishedDate, Url, and Title: let dataTemplate = { publishedDate: "", Url: "", Title: "", } dataTemplate.publishedDate = xml.chi ...

What can be done to ensure that the value from a promise is retained and available for use in

Currently, I am executing a database query and then manipulating the data to obtain a single numeric value. This value is essential for use in a subsequent array. The current definition of the array appears as follows (see below). The issue at hand is tha ...