-1

In plain html the following results in a space between the two buttons, but in React it doesn't:

  <button> Button 1 </button>

  <button> Button 2 </button>

I'm having to resort to the following to get a space.

  <button> Button 1 </button>
  {' '}
  <button> Button 2 </button>

This is unexpected. And before anyone gives CSS solutions, note that the following also results in no spaces between the output:

{'firstname'}

{'lastname'}

Gives:

firstnamelastname

What is the normal way to have spaces (ideally the HTML does it)?

I've also found that this type of interpolation can work, but feels awful to me:

  {`
    ${this.date()}
    - 
    ${this.verb()} @
    ${this.time()}
    -
    flight
    ${this.props.flight.airline.codeIcao}
    ${this.props.flight.number}
     - 
    ${this.props.flight.airport.codeIcao}
 `}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
pixelearth
  • 13,674
  • 10
  • 62
  • 110
  • 4
    That's *not* unexpected. See https://github.com/facebook/react/issues/1643#issuecomment-45148887 – jonrsharpe Jun 12 '19 at 18:04
  • 1
    @jonrsharpe You see the number of downvotes there? It IS unexpected for people new to React. Regardless of what is expected, the question remains of how to actually GET the spaces. – pixelearth Jun 12 '19 at 18:12
  • 1
    You know how to get them; you show it in your question. – jonrsharpe Jun 12 '19 at 18:13
  • @jonrsharpe the question is what is the "normal" or right way to do this. But you know what the question is. If the way I'm doing it is "normal", you could have just said that. – pixelearth Jun 12 '19 at 18:24
  • And that's what he said, by linking the appropriate reference! – Emile Bergeron Jun 12 '19 at 18:50
  • 2
    Possible duplicate of [Best practice when adding whitespace in JSX](https://stackoverflow.com/questions/40264084/best-practice-when-adding-whitespace-in-jsx) – Emile Bergeron Jun 12 '19 at 18:55
  • Your example isn't even related to JSX, it's just a simple string interpolation, which could feel less awful with a little destructuring. `${this.date()} - ${this.verb()} @ ${this.time()} - flight ${airline.codeIcao} ${flight.number} ${airport.codeIcao}` – Emile Bergeron Jun 12 '19 at 19:05

3 Answers3

2

you can simply use

{`${firstName} ${lastName}`}  // firstName and lastName are variables holding the actual value

If you are using any html elements like div, button, etc then you need to use css classes to manage the margin of elements.

0

if you want a space in pure HTML what about &nbsp; ?

          <div>
            <button>
              text1
            </button>
            &nbsp;
            <button>
              text1
            </button>
          </div>

Working example on stackblitz : demo

mickdev
  • 2,765
  • 11
  • 15
0

you can use &ensp; and &emsp; to add two and four non-breaking spaces