Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render curly braces as plain text in React/JSX

I am having problems displaying { and } as text in React. I saw a similar question that someone said to wrap the entire string in curlies, but this is not working:

let queries_block = this.state.previous_queries.map((dataset) => {             return (<p>{"{{}}"}<p>)         });          if (results) {             results_block = (                 <div>                     <p>Queries:</p>                     {queries_block}                     <br/><br/>                     <p>Results: {results_count}</p>                     <JSONPretty id="json-pretty" json={results}></JSONPretty>                 </div>             );         } else {             results_block = null;         } 

The return (<p>{"{{}}"}<p>) causes

ERROR in ./src/components/app.js Module build failed: SyntaxError: Unexpected token, expected } (47:13)    45 |                     <JSONPretty id="json-pretty" json={results}></JSONPretty>   46 |                 </div> > 47 |             );      |              ^   48 |         } else {   49 |             results_block = null;   50 |         }   @ ./src/index.js 15:11-38 webpack: Failed to compile. 

Is there an easy way to escape curly braces in jsx?

like image 664
codyc4321 Avatar asked Oct 06 '17 22:10

codyc4321


People also ask

How do you use curly braces in JSX?

How braces { } are used. Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

What does curly braces mean in JSX?

The curly braces are a special syntax to let the JSX parser know that it needs to interpret the contents in between them as JavaScript instead of a string. You need them when you want to use a JavaScript expression like a variable or a reference inside JSX.

What is ${} in JSX?

You use ${} in string templates like const k = `my name is ${name}` you use {} whenever you want to put some Java code in your JSX code like <div attr={name}>{name}</div>


1 Answers

If you'd like to render curly braces as plain text within a JSX document simply use the HTML character codes.

Left Curly Brace { : &#123;

Right Curly Brace } : &#125;

like image 131
Rob LaFave Avatar answered Sep 22 '22 09:09

Rob LaFave