Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>) 
like image 362
PrimeLens Avatar asked Oct 06 '15 20:10

PrimeLens


People also ask

How do you escape quotes in JSX?

Unlike string literals in JavaScript, string literals within JSX attributes can't contain escaped quotes. If you want to have e.g. a double quote within a JSX attribute value, you have to use single quotes as string delimiter.

How do you write if condition in JSX?

We can embed any JavaScript expression in JSX by wrapping it in curly braces. But only expressions not statements, means directly we can not put any statement (if-else/switch/for) inside JSX.

Does JSX allows us to write HTML in React?

JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.

How do you display text in React JS?

If you want to display it in an alert window, you need to call a function to trigger the window. However, if you need to show the message in your render, you can use a state to manage that. If you need to toggle the text on button click, you just need to update the onButtonClickHandler function to this.


1 Answers

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>) 
like image 189
Nasrul Faizin Avatar answered Sep 27 '22 22:09

Nasrul Faizin