English 中文(简体)
How to add a <br> tag in reactjs between two strings?
原标题:

I am using react. I want to add a line break <br> between strings

No results and Please try another search term. .

I have tried No results.<br>Please try another search term.

but it does not work, I need to add the <br> in the html.

Any ideas how to solve it?

   render() {
       let data = this.props.data;
       let isLoading = this.props.isLoading;
       let isDataEmpty = Object.entries(data).length === 0;
       let movieList = isLoading ? <Loader /> : isDataEmpty ?  No results. Please try another search term.  :
           Object.entries(data).map((movie, index) => <MovieTile key={index} {...movie[1]} />);
       return (
           <div className= movieList >{movieList}</div>
       );
   }
最佳回答

You should use JSX instead of string:

<div>No results.<br />Please try another search term.</div>

Because each jsx should have 1 wrapper I added a <div> wrapper for the string.

Here it is in your code:

render() {
   let data = this.props.data;
   let isLoading = this.props.isLoading;
   let isDataEmpty = Object.entries(data).length === 0;
   let movieList = isLoading ? <Loader /> : isDataEmpty ? <div>No results.<br />Please try another search term.</div> :
       Object.entries(data).map((movie, index) => <MovieTile key={index} {...movie[1]} />);
   return (
       <div className= movieList >{movieList}</div>
   );
}
问题回答

You can use CSS white-space to solve the problem.

React Component

render() {
   message = `No results. 
 Please try another search term.`;
   return (
       <div className= new-line >{message}</div>
   );
}

CSS

.new-line {
  white-space: pre-line;
}

OUTPUT

No results.
Please try another search term.

break text to line:

render() {
  ...
  <div>
    {this.props.data.split( 
 ).map( (it, i) => <div key={ x +i}>{it}</div> )}
  </div>
  ...

Some HTML elements such as <img> and <input> use only one tag. Such tags that belong to a single-tag element aren t an opening tag nor a closing tag. Those are self-closing tags.

In JSX, one has to include the slash. So, remove <br> and try <br />

Here is how I got around this. Let message be the prop/variable that has the string containing line breaks to be displayed in HTML as follows:

message =  No results.<br>Please try another search term. ;
<div>
  {message}
</div>

To make this work, we need to use instead of break tag <br> and set the following css on the wrapper element of this message as follows:

message =  No results.
Please try another search term. ;
<div className="msg-wrapper">
  {message}
</div>

CSS:

.msg-wrapper {
  white-space: pre-wrap;
}

OUTPUT:

No results.
Please try another search term.

If you don t want put the string inside a <div> you could use <> to do it.

Like this:

var text = <>This is a text in the first line;<br />this is a text in a second line</>;

Just split text by /n, I do this in this way:

<div>
    {text.split( 
 ).map((item, i) => <p key={i}>{item}</p>)}
</div>

Try with span

return (
           <div className= movieList ><span>{movieList}</span></div>
       );

If you are like in my situation and you don t want to add css, you can do that :

render () {
...
return (
...
<Typography component="p">
...
{(contact.lastname)?<div>Hello {contact.firstname} {contact.lastname}</div>:  }
...
</Typography>
...
);
}

using `
worked for me however i am not sure if it is the exact solution to the problem :

 import React from  react ;
import ReactDOM from  react-dom ;

let element = (
<div>
  <h1> Hello world</h1>
  This is just a sentence <br></br>
  But This line  should not be in the same previous line. <br></br>
  The above content proves its working. <br></br>
  npm v6.14.6 | react : {React.version} 
</div>
);

ReactDOM.render(element,document.getElementById("html-element-id"))

You can add a span tag and add block as a class.

Pomodoro Technique Timer <span className="block">with Bla</span>

The simplest thing which I did is by creating a component.

const EmptySpace = ({ spaceCount = 0 }) => {
  return (
    <>
      {Array.from({ length: spaceCount }, (item, index) => {
        return <br key={index} />;
      })}
    </>
  );
};

export default EmptySpace;
<EmptySpace spaceCount={1} />

In your case you could do something like this:

const msg = (
    <p>
      No results <EmptySpace spaceCount={2} />
      Please try another search term.
    </p>
  );

can pass the content as an array ref: https://reactjs.org/docs/jsx-in-depth.html

Page

import MyComponent from  ./components/myComponent ;

export default function Page() {
  return(
        <div>
            <MyComponent
              content= {["Line 1", <br/>,  "Line 2", <br/>, "Line 3"]}
            />
        </div>
  )
}

React Component

export default function MyComponent(props) {
  return (
    <p>
        {props.content}
    </p>
  )
}

OUTPUT

Line 1
Line 2
Line 3




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签