Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add whitespaces in React JSX

I have this li tag

<li className='u-cursor--pointer u-padding-vertical-small c-start-retro-line'
  key={project.get('id')}
  onClick={() => this.handleProjectSelection(project.get('id'))} >
  <i className='fa fa-square' style={{color: project.get('color')}}></i>
  {project.get('name') === 'default' ? 'No Project' : project.get('name')}
</li>

And I need to add an space between the <i></i> tag and what's inside the {} {project.get('name') === 'default' ? 'No Project' : project.get('name')}

How can I do this? I tried <span></span> but It doesn't work (I need an extra space, not a new line)

like image 553
Liz Parody Avatar asked Jan 20 '18 00:01

Liz Parody


People also ask

How do you put a space in code?

The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as &nbsp; or &#160;.

How do you use BR in React?

Using <br> Tag in ReactWe use the <br> tag in HTML to break the string in between or break the section; hence, if you want to break the string, the <br> tag will be placed, and the followed content will move to the next line. For example, there is one string, and you can use the <br> , as given below.

Can keys have spaces React?

React is clever enough to deal with spaces or any special characters. Since it uses keys only to identify objects, it should treat them as a black box. Only bad code cannot deal with spaces.


1 Answers

You can try:

1) Either {' '} or {" "}:

<li>
    <i className="..."></i>
    {' '}my text
</li> 

2) Simply use the HTML entity &nbsp;:

<li>
    <i className="..."></i>
    &nbsp;my text
</li> 

3) Alternatively, you could insert spaces in the strings:

<li>
    <i className="..."></i>
    {project.get('name') === 'default' ? ' No Project' : ` ${project.get('name')}`}
</li>

In the last example, notice the use of string substitution.

like image 155
inostia Avatar answered Sep 23 '22 22:09

inostia