-
Notifications
You must be signed in to change notification settings - Fork 64
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
Trivial else (suggestion/enhancement) #57
Comments
Thanks for the request! Are the one-liners usually strings or very simple statements though? Unless I'm missing something you could do all your examples pretty easily with a custom component, |
You're thinking only of the one-liner, the “lots of stuff” part will be more complex and I wouldn't want it to evaluate if the condition is true (or false in the inverted syntax case). |
Ah yeah that's true, but I still find it kinda weird. Happy to hear thoughts from others. |
In my opinion the succinct version would be the flat if-else.
Discussion #32 |
Yeah, that would in theory work for me, but I can also understand the objections of “non-xml-ness”. This one is legal on the parser level, and won't break lints. For me if it gets complex enough — if both branches have jsx beyond a single self-closer, or any js more complex than you could fit in half a line — you're better off with . This would be really an abbreviation for a case that, at least for me, is common… and it's not because I'm lazy, but because the full construct really obscures the code. |
BTW I do have a modicum of experience in this sort of stuff, which is why I became interested 😉 in 1999 I co-designed the second-gen Zope template language (ZPT), which compared to the old one was a move in the direction of more standard XML. Just to contextualise where I'm coming from… So. After a few days thinking about this (and a bit of reading through old issues)… I think <If condition={foo}>
bar
<Else>qux</Else>
baz here is technically legal too but discouraged
</If> Then the old syntax can be supported for compatibility… although, I suppose that's what major versions are for, at some point (4.0.0?) it can be dropped. As for the “trivial” bit, I'd favour making both <If condition={wantFoo} then="foo" else="bar"/> {/*legal*/}
<If condition={wantFoo} else={<NotFound/>}>foo</If> {/*legal but weird but legal*/}
<If condition={wantFoo} else="bar">foo<Else>bar</Else></If> {/*not legal*/}
<If condition={wantFoo} then="foo" else="bar">qux</If> {/*not legal*/}
<If condition={wantFoo} then="foo">bar</If> {/*not legal?*/}
<If condition={wantFoo} then="foo" else>bar</If> {/*legal???*/} |
I don't think that the deprecated Else-Syntax needs to be revived. In your suggested version it is not as readable as it should be: Regarding the abbreviated syntax, you already name the counter-argument: Edge cases which make less and less sense. Additionally, one of the most prominent React arguments is: Keep the API surface as small as possible. And React really lives up to this mantra, if you think about it. I'm also not convinced that writing
is more readable than, e.g.
From my point of view I would always expect the else branch on the same level as the if branch. I can't imagine that users wouldn't be confused by the abbreviated syntax... |
I find I have a common pattern of
It kind of makes me want to just use a ternary; the control statements are neither succinct or clear/intent-expressive.
The one-liner in question will be a marker for “field not present”, an icon, a component, and in some cases even a single space.
An alternative which I think would make sense is something like:
While having jsx in props looks weird, it's completely valid, and I've used it a couple of times with react-bootstrap stuff. I wouldn't recommend it for longer values, but it's fine for short stuff. So, you could in fact also have,
someprop={<NotFound/>}
.Now, assuming this is a good idea at all, what does the actual tag and prop look like?
My first instinct was to add this to :
The problem should be obvious though… having “else” before “then” feels really odd. But I don't know if that objection is strong enough to justify loading the namespace with a new tag that does mostly the same thing as .
Here's a weird alternative, I don't like but I'll write it down anyway:
Anyway. Please discuss 😺
The text was updated successfully, but these errors were encountered: