Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaping curly brackets #1545

Closed
RickWong opened this issue May 17, 2014 · 16 comments
Closed

Escaping curly brackets #1545

RickWong opened this issue May 17, 2014 · 16 comments

Comments

@RickWong
Copy link

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> 
@syranide
Copy link
Contributor

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 }'}.

@sophiebits
Copy link
Collaborator

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

@RickWong
Copy link
Author

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:

@syranide
Copy link
Contributor

@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.

@RickWong
Copy link
Author

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.

@mysterycommand
Copy link

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.

@RickWong
Copy link
Author

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

@syranide
Copy link
Contributor

Related facebook/jsx#28

@darobin
Copy link
Contributor

darobin commented Aug 13, 2015

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

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

@timurtu
Copy link

timurtu commented Feb 12, 2017

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 '}'
}

@gaearon
Copy link
Collaborator

gaearon commented Feb 12, 2017

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

@prigara
Copy link

prigara commented Feb 13, 2017

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

@mnpenner
Copy link
Contributor

mnpenner commented Feb 8, 2019

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>'

@tobq
Copy link

tobq commented May 24, 2019

What if you actually want a \ followed by a {

JSX\\\{ -> Out\{

@hacknlove
Copy link

my solution, define the whole string outside

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

@powelski
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests