Why do people put { “ ” } in their React / JSX?

☆樱花仙子☆ 提交于 2020-02-26 11:18:34

问题


I have this in some React documentation, as well as screencasts. People will write { " " } inside of their JSX templates.

Why do they do this? It looks like they are using it as an alternative to line breaks, but I don't see that explicitly explained anywhere.


回答1:


This is used to put an explicit space in a text block, since leading and trailing spaces are ignored at compile/transformation time when there is another tag.

Example:

<div>
 Text
 <a>some Text</a>
</div>

Will result with Textsome Text on the screen. (see the missing space)

<div>
 Text{' '}
 <a>some Text</a>
</div>

Will result as wanted with Text some Text on the screen.




回答2:


It is used to add some leading or trailing spaces

render() {
  return ( <div>
            My name is{' '}
            <span> Piyush </span>
           </div>
          )
}


来源:https://stackoverflow.com/questions/38547558/why-do-people-put-in-their-react-jsx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!