Javascript React JSX,如何用单引号渲染文本?示例 <p>我已经</p>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32979512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:27:45  来源:igfitidea点击:

React JSX, how to render text with a single quote? Example <p>I've</p>

javascriptreactjsreact-jsx

提问by PrimeLens

In React JSX how can I have the following text include a single quote? Or other punctuation that might need escaping?

在 React JSX 中,如何让以下文本包含单引号?或者其他可能需要转义的标点符号?

 return (
   <div>
     <p>I've seen the movie.</p>
   </div>     
 )

回答by Nasrul Faizin

return (
   <div>
     <p>{"I've seen the movie."}</p>
   </div>
 )

回答by PrimeLens

Nevermind, it works as is.

没关系,它按原样工作。

It was the IDE that was highlighting it as a mistake

是 IDE 将其突出显示为错误

回答by abhishek kasana

You can use &quot html entity to have quote in your text.

您可以使用 " html 实体在文本中引用。

<Text>I&quot;ve seen the movie.</Text>

output: I"ve seen the movie.

输出:我看过电影。

or if want single quot use the below option:

或者如果想要单引号,请使用以下选项:

<Text> I&apos;ve seen the movie.</Text>

<Text>{'I\'ve seen the movie.'}</Text>
{/* you can use both ticks and single quotes depending on your use. */}
<Text>{`I've seen the movie.`}</Text>
<Text> I&apos;ve seen the movie.</Text>

<Text>{'I\'ve seen the movie.'}</Text>
{/* you can use both ticks and single quotes depending on your use. */}
<Text>{`I've seen the movie.`}</Text>

output: I've seen the movie.

输出:我看过电影。

回答by Gregg B

This is a great reason to use the backtick (`) for strings where it makes sense.

这是`对有意义的字符串使用反引号 ( )的重要原因。

The text in the original question will work fine even though the syntax highlighting is off, but by also moving strings to a constant you can avoid worrying about escaping, highlighting and they're easier to find/update.

即使关闭了语法突出显示,原始问题中的文本也能正常工作,但通过将字符串移动到常量,您可以避免担心转义、突出显示,并且它们更容易查找/更新。

const TEXT_FOR_MOVIE = `Some text that's "quoted"`

const TEXT_FOR_MOVIE = `Some text that's "quoted"`

const TEXT_FOR_MOVIE = Some text that's "quoted"

const TEXT_FOR_MOVIE = Some text that's "quoted"