React JSX, how to render text with a single quote? Example <p>I've</p>
JavascriptReactjsJavascript 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, '
:
return (<p>I'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"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.
}
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}"`}