react icon indicating copy to clipboard operation
react copied to clipboard

Escaping curly brackets

Open RickWong opened this issue 12 years ago • 16 comments

Thanks for React and JSX!

Would it be possible to escape curly brackets in the future? Like this:

<div>In JSX you can use the \{ and \} characters to escape and evaluate inline JavaScript.</div> 

RickWong avatar May 17 '14 12:05 RickWong

You bring up a valid issue.

While escaping with a back-slash would be possible, it could introduce far more serious issues. What if you actually want a \ followed by a {. \\{ is basically the only solution and the only proper implementation would be to fully support escaping. Which doesn't really mesh with HTML IMHO.

The simplest solution and the one I'm pretty certain the devs will advocate as well is {'{'} and {'}'} or rather {'{ and }'}.

syranide avatar May 17 '14 14:05 syranide

Yeah, quoting as a plain JS string is the way to do it. Let us know if this poses a problem for you somehow.

sophiebits avatar May 17 '14 18:05 sophiebits

Hey Spicyj, sadly it makes <style> tags with css { } blocks impossible to embed in JSX. I've worked my way around it by putting css in a property:

  • https://edealer.nl/mao/ (proof of concept http://suitcss.github.io -ish components)

RickWong avatar May 17 '14 23:05 RickWong

@RickWong If you really want have inline <style> (I really don't recommend it) then you should use dangerouslySetInnerHTML instead and define the CSS string elsewhere.

syranide avatar May 17 '14 23:05 syranide

Hehe, it's inline but it makes the component self-contained. It's almost comparable to how JSX is inline HTML. My work around does utilize dangerouslySetInnerHTML but it's not optimal as it looks like crap instead of a template.

Problem with the other solution {'{'} is that those create span tags which invalidate the CSS syntax.

RickWong avatar May 17 '14 23:05 RickWong

~~Maybe you could just use HTML entities? &#123; (opening curly brace) and &#125; (closing) … doesn't solve the "looks like crap" problem, but might work?~~

Nope, it looks like it works in renderToStaticMarkup but when it's just render'd, it still gets the spans.

mysterycommand avatar Feb 24 '15 00:02 mysterycommand

Old topic but I think JS string literals are actually a valid solution to this problem. Also I solved it by putting them all in a "sheet" prop that I use instead of "children". See https://github.com/RickWong/ReactStyle

RickWong avatar Feb 24 '15 09:02 RickWong

Related facebook/jsx#28

syranide avatar Feb 24 '15 09:02 syranide

FWIW, for anyone stumbling upon this issue while searching for a solution, the simplest (assuming ES6) is probably:

<style>{`
    .spinner ellipse { opacity: 0; }
    /*...*/
`}
</style>

darobin avatar Aug 13 '15 12:08 darobin

In webstorm atleast {'}'} gives parsing issues and makes the code reformat incorrectly. My solution to this which I wasn't really happy about was

function curlyStart() {
  return '{'
}

function curlyEnd() {
  return '}'
}

timurtu avatar Feb 12 '17 00:02 timurtu

Please file an issue with WebStorm, I'm sure they'd be happy to fix. cc @prigara

gaearon avatar Feb 12 '17 01:02 gaearon

Hi, @timuric! Here's a related issue on our tracker: https://youtrack.jetbrains.com/issue/WEB-21774 Hope we'll get it fixed soon.

prigara avatar Feb 13 '17 09:02 prigara

If you happen to be generating JSX like me, this seems to work:

> '{hello}{world}'.replace(/([{}]+)/g,"{'$1'}")
"{'{'}hello{'}{'}world{'}'}"

Or maybe this fun little regex:

> 'hello <style>a { font-weight: bold }</style>'.replace(/(<style>)(.*?)(<\/style)/gis, (_,start,css,end) => `${start}{String.raw\`${css.replace(/`/,'\\`')}\`}${end}`)
'hello <style>{String.raw`a { font-weight: bold }`}</style>'

mnpenner avatar Feb 08 '19 05:02 mnpenner

What if you actually want a \ followed by a {

JSX\\\{ -> Out\{

tobq avatar May 24 '19 18:05 tobq

my solution, define the whole string outside

const codeExample = '{ _id: { $gt: ObjectId() } }'
return (
<foo>{codeExample}</foo>
)

hacknlove avatar Mar 17 '20 11:03 hacknlove

In my opinion, it would make a lot of sense to be able to escape with \{. I know there's a case when we actually need the \{ string and I see nothing wrong with having it escaped as \\\{. This is a very rare case, while having curly braces in strings is common. Having } in the template as {'}'} is completely unreadable. That's my 2 cents.

powelski avatar Jun 22 '21 15:06 powelski