问题
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