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

JavascriptReactjs

Javascript Problem Overview


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

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

Javascript Solutions


Solution 1 - Javascript

Render as a JS string literal (with double-quotes):

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

... or use the HTML entity for an apostrophe, &apos;:

return (<p>I&apos;ve seen the movie.</p>)

Solution 2 - Javascript

Nevermind, it works as is.

It was the IDE that was highlighting it as a mistake

Solution 3 - Javascript

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

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

output: I"ve seen the movie.

or if want single quot use the below option: > I've seen the movie. > > {'I've seen the movie.'} > {/* you can use both ticks and single quotes depending on your use. */} > {I've seen the movie.}

output: I've seen the movie.

Solution 4 - Javascript

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"

Solution 5 - Javascript

In case if you want to show a variable with quotes you can use this it will show quote and also display the value of the variable

{`"${variable}"`}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrimeLensView Question on Stackoverflow
Solution 1 - JavascriptNasrul FaizinView Answer on Stackoverflow
Solution 2 - JavascriptPrimeLensView Answer on Stackoverflow
Solution 3 - Javascriptabhishek kasanaView Answer on Stackoverflow
Solution 4 - JavascriptGregg BView Answer on Stackoverflow
Solution 5 - JavascriptSehrish WaheedView Answer on Stackoverflow