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

JSX Compiler interpolation alternatives. #51

Open
DylanPiercey opened this issue Feb 10, 2016 · 2 comments
Open

JSX Compiler interpolation alternatives. #51

DylanPiercey opened this issue Feb 10, 2016 · 2 comments
Labels
Proposal 2.0 Proposals considerable for JSX 2.0

Comments

@DylanPiercey
Copy link

DylanPiercey commented Feb 10, 2016

This will likely never change as it is far too late but it bothers me so I want to at least mention it.

I think that using curlies "{}" for interpolation inside JSX is confusing, unintuitive and ugly. Typically curlies refer to an object or a block neither of which can actually exist inside the interpolation within JSX.

Parens "()" are typically used to enclose an expression which is exactly what is going on in JSX.

I propose that we switch from this:

<div a={ { b: 1 } }/>

Which is confusing for beginners because it looks like an object in an object, or if you are coming from mustache something different entirely.

To:

<div a=( { b: 1 } )/>

Here it would be easy to explain that, like everywhere else in javascript, parans are used to represent an expression which in this case returns an object to the attribute of the virtual div.

Just my thoughts, feel free to discuss or close.


This was originally posted here: https://phabricator.babeljs.io/T7074

@sebmarkbage sebmarkbage mentioned this issue Oct 12, 2016
@yiminghe
Copy link

yiminghe commented Oct 25, 2016

can just be simplified to

<div x={a:1}>
const spreadReg = /^\.\.\.[\w$_]/;
const objReg = /^[\w$_](?:[\w$_\d\s]+)?:/;
const es2015ObjReg = /^[\w$_](?:[\w$_\d\s]+)?,/;


// {a:1}, {...a}, {a,b} not {[a,1,2]}, {x}, { x + 1 }
function isObject(str) {
  return str.match(spreadReg) || str.match(objReg) || str.match(es2015ObjReg);
}

@DylanPiercey
Copy link
Author

@yiminghe my issue was never the simplicity in terms of compactness, but the simplicity in terms of explaining it to others.

Like I said, in javascript curly braces are used to denote blocks/scopes and objects. Anything that can be wrapped in parentheses in javascript must be an expression. In React the interpolation is always an expression so I think it would make sense to make the syntax look such that you were dealing with an expression.

Looks like @sebmarkbage put this down as a consideration for [email protected] which would be sweet.

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

No branches or pull requests

3 participants