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?
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.
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.
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>
If you'd like to render curly braces as plain text within a JSX document simply use the HTML character codes.
Left Curly Brace { : {
Right Curly Brace } : }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With