Best practice when adding whitespace in JSX

前端 未结 9 787
粉色の甜心
粉色の甜心 2020-12-02 11:35

I understand how (and why) to add a whitespace in JSX, but I am wondering what\'s best practice or if any makes any real difference?

Wrap both elements in a

相关标签:
9条回答
  • 2020-12-02 12:12

    I have been trying to think of a good convention to use when placing text next to components on different lines, and found a couple good options:

    <p>
        Hello {
            <span>World</span>
        }!
    </p>
    

    or

    <p>
        Hello {}
        <span>World</span>
        {} again!
    </p>
    

    Each of these produces clean html without additional &nbsp; or other extraneous markup. It creates fewer text nodes than using {' '}, and allows using of html entities where {' hello &amp; goodbye '} does not.

    0 讨论(0)
  • 2020-12-02 12:16

    I tend to use &nbsp;

    It's not pretty but it's the least confusing way to add whitespace I've found and it gives me absolute control over how much whitespace I add.

    If I want to add 5 spaces:

    Hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span className="second-word-formatting">World!</span>

    It's easy to identify exactly what I'm trying to do here when I come back to the code weeks later.

    0 讨论(0)
  • 2020-12-02 12:25

    use {} or {``} or &nbsp; to create space between span element and content.

    <b> {notif.name} </b> <span id="value"> &nbsp;{ notif.count }{``} </span>
    
    0 讨论(0)
  • 2020-12-02 12:26

    If the goal is to seperate two elements, you can use CSS like below:

    A<span style={{paddingLeft: '20px'}}>B</span>
    
    0 讨论(0)
  • 2020-12-02 12:31

    Because &nbsp causes you to have non-breaking spaces, you should only use it where necessary. In most cases, this will have unintended side effects.

    Older versions of React, I believe all those before v14, would automatically insert <span> </span> when you had a newline inside of a tag.

    While they no longer do this, that's a safe way to handle this in your own code. Unless you have styling that specifically targets span (bad practice in general), then this is the safest route.

    Per your example, you can put them on a single line together as it's pretty short. In longer-line scenarios, this is how you should probably do it:

      <div className="top-element-formatting">
        Hello <span className="second-word-formatting">World!</span>
        <span> </span>
        So much more text in this box that it really needs to be on another line.
      </div>
    

    This method is also safe against auto-trimming text editors.

    The other method is using {' '} which doesn't insert random HTML tags. This could be more useful when styling, highlighting elements, and removes clutter from the DOM. If you don't need backwards compatibility with React v14 or earlier, this should be your preferred method.

      <div className="top-element-formatting">
        Hello <span className="second-word-formatting">World!</span>
        {' '}
        So much more text in this box that it really needs to be on another line.
      </div>
    
    0 讨论(0)
  • 2020-12-02 12:31

    You can add simple white space with quotes sign: {" "}

    Also you can use template literals, which allow to insert, embedd expressions (code inside curly braces):

    `${2 * a + b}.?!=-` // Notice this sign " ` ",its not normal quotes.
    
    0 讨论(0)
提交回复
热议问题