-
Notifications
You must be signed in to change notification settings - Fork 132
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
Crazy Idea: Deprecate JSXText? #35
Comments
Sorry for the tl;dr and loosly structured thoughts, but I hope most of what I'm trying to convey/play with is understandable. #8 sums up some of my thoughts on it I think 👍 I think it's great for many reasons, including being more transparent about strings not being concatenated up-front and encouraging users to do it explicitly rather than always go for the "beautiful syntax". Also no more arbitrary whitespace! Although #28 is another possibility for that it's not nearly as intuitive or as explicit.
I'm really not a fan of this, I'm kind of OK with comma, but it's really weird that it's not used between JSX elements (but somewhat understandable). I wonder if commas between JSX elements really is such a bad idea, there are benefits and perhaps the aversion to it has more to do with familiarity with HTML than practicality. Else, for commas themselves, I feel like it's commas unavoidable to an extent even if syntax wasn't an issue. Writing An alternative to commas is to make newline be implicit commas (kind of like semicolons in JS). That way you largely avoid requiring commas, but you can still use them to stack multiple values per line. Still not a huge fan and doesn't translate nicely to other languages. JSX elements are pretty much named-argument + variable-argument functions in a technical sense:
I think it could make a lot of sense to move more towards such a model, but not for the syntax itself. It needs to be easily nested without causing an aneurysm. But I think it could make sense to rethink and derive JSX from that rather than HTML. Especially because JSX as it is right now tricks people into sending strings for static arguments. So just to run blindly ahead without any critical thinking:
The idea being that what's on the right-side of And perhaps the same idea should be applied to children as well so that we end up with:
Which I'm probably a more OK with (but it's not really encouraging concat though). Substituting Instinctively I feel like there should be a consistent rule, either we separate everything by Tangentially I still think facebook/react#3473 (comment) is the way to go eventually, i.e. renderable values must be elements too (frontends without style inheritance are unaffected, so it's not as bad as it sounds). In that world you would never have primitives in your children which simplifies the situation. So taking it all the way and then some, this is what I envision it could look like (including trying to be nice with HTML):
EDIT: I'm not sure I would call "no curlies" a good thing in your example, I feel like you'd need to wrap the ternary operator in parenthesis instead then for it to be humanly readable. |
@sebmarkbage I like your solution. It's more clear than handlebars-style below. And it's moving us to js-code from string-templates paradigm. |
It seems a bad idea. Why not semicolon? I think if tag content treated as js code, it's should use semicolon after each expression. It would be nice if jsx components could be use not only for html markup, but as any code parser. var span = <span className="header">Plain text goes here</span>;
var css = <LessHndr /* IDE code highlighter instruction goes here */>
@header-font: Georgia;
h1, h2, h3, h4 {
font-family: @header-font;
}
</LessHndr>
var product = <SQLMigration /* IDE code highlighter instruction goes here */>
id: INT(10), null(true), autoincrement(true)
price: FLOAT(7,2), null(true), default(null)
</Migration> What if JSX would support var content = <SomeComponent:>
console.log("OK!");
</SomeComponent> |
Because semicolons are used to denote statements while commas are used for expressions. |
Why not like this: var box =
<Box>
"Leading text";
functionCall();
<Answer value={'this is': 'an object'}>"yes"</Answer>;
<Box.Comment><Child /></Box.Comment>;
anotherFunctionCall();
<OptionalTrailingComma />;
expr + ession;
</Box>; ? |
As I said, then it'd be assumed that you can put any statement in there when you can't, like: var box = <Box>
var foo = "bar";
</Box>; |
Also it conflicts with the semantics of statement lists everywhere else in JavaScript, while with commas you can just reason that it's calling |
I'm agreed that commas is more correct according to semantics. var box =
<Box>
"Leading text"
functionCall()
<Answer value={'this is': 'an object'}>"yes"</Answer>
<Box.Comment><Child /></Box.Comment>
anotherFunctionCall()
<OptionalTrailingComma />
(expr + ession)
</Box>; Because in this context commas are using only for one reason - concatenation. |
Introduces too much syntax ambiguity. You could maybe get away with making On Sunday, 17 May 2015, Denis Izmaylov [email protected] wrote:
Sebastian McKenzie |
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed. |
JSX content could be considered do expressions, in which case, semi-colons would make sense and it would make conditionals easier to express. var box = <Box>
if (foo)
foo
else
<Placeholder />
</Box>; The confusing part would be that each statement would be its own block so that you could do multiple return values. var box = <Box>
let x = 1;
let y = 2;
x + y;
</Box>; That would be |
If those were three separate blocks, then the x+y block wouldn't have acces to x or y bindings |
Might it make sense to relax the delimiting rules so that
It's not consistent with how arrays/objects are in JS, although I would personally enjoy implied commas for those as well actually (if it can be neatly solved), but that is an entirely different battle with ECMA. |
I feel kind of weird saying this since I am a huge advocate of expanding JSX to general purpose programming, but I feel that this change would shift JSX too far from its HTML roots and simply make it closer to just an alternative function call syntax without some of JSX's unique benefits. Having defended JSX to a fair amount of people who don't understand why we can't just use createElement(), one of my go to arguments is that JSX really shines when content is king. I love JSX precisely because I'm not having to put large paragraphs of text into strings, never worry about escaping quotes, etc. Additionally, I am allowed to escape the strict tree-children realities that functions imply. For example |
@tolmasky Remember that JSX has nothing to do with HTML, XML maybe. HTML is only one possible outcome, you have ReactNative and a host of other renderers which has nothing to do with HTML. That being said your points are valid. IMHO, I think a large part of the friction comes from the general mindset of HTML, where you mix both the large and complex with the small and simple. I.e. you design both large visual hierarchies (the visual structure) AND documents (i.e. flowing content with images, text, etc) with the same language and primitives. For documents JSX can be kind of heavy-handed and many times markdown or similar syntax could be a better fit, or some alternate syntax. Whereas when you do complex hierarchies you don't care about textual content. HTML is a document-language and so it's unsurprising that you get a lot of both. But if you look at interfaces generally you'll find a much more rigid structure and even inline text isn't even a thing, there are no inherited styles and thus "text" can't just be drawn anywhere. You then have different components for different purposes, some for plain basic text, some for rich text, some for complex documents. So you naturally get components of the type The way JSX currently works also requires special white-space handling rules, but there's no perfect solution facebook/react#4134, unexpected behaviors are bad. |
@tolmasky The main motivator for me is that when you're building large products you often have internationalization requirements. There are essentially three variants that are common:
Option 3 seems to be very rare. The common solutions are either 1 or 2 - which is how Facebook, AirBnB, Yahoo etc. use React. If your project uses special annotations for text content anyway, then it is actually very rare that you embed text in your regular JSX. In fact, at Facebook we even have a special form of JSX for translations. The primary motivation is that it's nice to have but not actually used that much in its pure form. Of course, there are many more smaller companies that build apps that never see internationalization needs, and there are also lots of internal apps at bigger companies that never need translation because they're only using the corporate default language. There are also big companies that build separate apps for each region. So it becomes a tradeoff between these two worlds. It would be nice to have something that works well in both scenarios though. |
Please, do not write yoda-ternary-expressions. Bad: const ex = (
<div>
{checkYourCondition(value, expectedResult) ?
<First value={value}>Example component</First> :
<SecondComponent notReadable />}
</div>
) That form unintuitive. You can't at the first sight understand that this ternary expression. Good: const ex = (
<div>
{
checkYourCondition(value, expectedResult)
? <First value={value}>Example component</First>
: <SecondComponent notReadable />
}
</div>
) |
I L O V E I T !please make it real |
Am I the only one in favor on commas? I would think of the syntax as sugar for arrays and the only thing missing is the ‘[‘ and ‘]’ where as semicolons look like a code block - so it’s further from what’s its sugar for. New lines are ambiguous and likely to confuse. |
Currently we allow arbitrary text content to flow amongst tags. However, we have noticed that while building UIs, you actually don't utilize this very much. @syranide can speak more to this.
You usually want to wrap your text content in some other container. E.g. instead of writing it like this:
You should write something like this:
At the very least you want to wrap it for internationalization purposes:
What if we only treated it as a sequence of expressions and got rid of the curlies? We don't really need curlies around attributes since they can disambiguated with parenthesis if necessary.
That way the JSX example would become:
No curlies!
Multiple JSXChildren have to be delimitated by
,
. A JSX elements doesn't need to be followed by a comma.This moves the design further away from HTML generation and towards UI composition.
The text was updated successfully, but these errors were encountered: