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:
JavaScript
x
18
18
1
let queries_block = this.state.previous_queries.map((dataset) => {
2
return (<p>{"{{}}"}<p>)
3
});
4
5
if (results) {
6
results_block = (
7
<div>
8
<p>Queries:</p>
9
{queries_block}
10
<br/><br/>
11
<p>Results: {results_count}</p>
12
<JSONPretty id="json-pretty" json={results}></JSONPretty>
13
</div>
14
);
15
} else {
16
results_block = null;
17
}
18
The return (<p>{"{{}}"}<p>)
causes
JavaScript
1
14
14
1
ERROR in ./src/components/app.js
2
Module build failed: SyntaxError: Unexpected token, expected } (47:13)
3
4
45 | <JSONPretty id="json-pretty" json={results}></JSONPretty>
5
46 | </div>
6
> 47 | );
7
| ^
8
48 | } else {
9
49 | results_block = null;
10
50 | }
11
12
@ ./src/index.js 15:11-38
13
webpack: Failed to compile.
14
Is there an easy way to escape curly braces in jsx?
Advertisement
Answer
I think the issue is just a typo. You have this:
JavaScript
1
2
1
return (<p>{"{{}}"}<p>)
2
but you need this (note the closing p
tag instead of another opening one):
JavaScript
1
2
1
return (<p>{"{{}}"}</p>)
2