Comments (9)
My opinion: the choose block or something with those semantics is the best choice for else/if... I don't really like how the first one works with else/if, it's a bit messy. Just as long as we can preserve backwards compatibility with the current syntax and add this as another way of expressing it.
from jsx-control-statements.
I'm really not sure how to go about this API. IMO and at first glance the cleanest choice would be to only support <If>
as standalone statement without <Else>
, while <Choose> / <Switch>
handle the other cases (of course, staying backwards compatible remains a core value, I'm just speaking about the ideal API, not about dropping support for <Else>
). But this approach isn't ideal either.
On the one hand choose
or switch
have the following pros:
- correct nesting and therefore correct indentation by IDE's
- simplified implementation
On the other hand we would introduce two idioms which solve the same thing. So the cons are:
- As user I expect
<If>
,<ElseIf>
and<Else>
as basic control statements, but the last two or the last one are not supported - If I start my implementation with an
<If>
and now need to add an<ElseIf> / <Else>
, then I'm forced to switch to<choose> / <switch>
syntax, which is just plain cumbersome - My personal experience with JSP's and XSLT - where you have to use
<choose>
- is not that good: It just feels clumsy to write that stuff
In any case, let's start a discussion. We shouldn't rush this.
from jsx-control-statements.
Trying to pull in some people and their opinions: @justafish @helarqjsc @vitalybe (who opened the issue about elseif) and @jimfb (who appears to watch over this lib)
from jsx-control-statements.
Some research. Currently we're using Handlebars style syntax, although Handlebars doesn't support elseIf (if I'm not mistaken):
{{#if x}}
IfBlock
{{else}}
ElseBlock
{{/if}}
JSP (or JSTL to be more precise) has to handle this differently, because it's based on XML:
// no elseIf or else supported
<c:if test="${x}">
IfBlock
</c:if>
// within choose the else block (otherwise) is mandatory
<c:choose>
<c:when test="${x}">
IfBlock
</c:when>
<c:otherwise>
ElseBlock
</c:otherwise>
</c:choose>
So depending on the background, different approaches have been taken. String based template languages like Handlebars implement what any developer would expect, while XML based templating languages need to adhere to correct nesting rules, which forces them to use the choose stlye.
JSX tries to emualte XML, so it fits the second category. However, since it transpiles to JS it is not confronted with the same restrictions.
My current thinking is to support both styles, knowing how cumbersome the choose style is.
from jsx-control-statements.
"choose" syntax looks good to me and I love the fact it makes indentation logical again.
To make sure, the idea is that if one condition is true, the others wouldn't execute even if their condition would also resolve truthfully? E.g, this:
<switch>
<case condition={true}>
<div>1</div>
</case>
<case condition={true}>
<div>2</div>
</case>
<default>
<div>3</div>
</default>
</choose>
Would only generate a single <div>1</div>
.
from jsx-control-statements.
Sure, it should and will behave as JS behaves. In any case it's going to be transformed into a nested / chained ternary operator. For your example: true ? <div>1</div> : true ? <div>2</div> : <div>3</div>
Are you also comfortable with choose | when | otherwise
? I think that we should reserve switch | case | default
for real switch statements.
Thanks for your feedback.
from jsx-control-statements.
The "choose" syntax is fine. Better option than switch.
On Wed, Jan 13, 2016, 21:28 texttechne [email protected] wrote:
Sure, it should and will behave as JS behaves. In any case it's going to
be transformed into a nested / chained ternary operator. For your example: true
?1: true ?2:3Are you also comfortable with choose | when | otherwise? I think that we
should reserve switch | case | default for real switch statements.Thanks for your feedback.
—
Reply to this email directly or view it on GitHub
#26 (comment)
.
from jsx-control-statements.
I'm finding myself preferring the choose
syntax also 👍
from jsx-control-statements.
See #32 for the better syntax.
from jsx-control-statements.
Related Issues (20)
- The control statements don't narrow TypeScript types HOT 1
- TypeScript type for children is missing a render function HOT 1
- TChildren conflicts with ReactNode children HOT 2
- Why do we need ```transform-react-inline-elements``` plugin ?
- Switch-case style control statement HOT 2
- Problem with ./node_modules/tsx-control-statements/components.ts HOT 3
- Typo error HOT 1
- Support babel macros HOT 2
- conditional statements don't work unless wrapped in brackets. is this supposed to happen? HOT 1
- Not working with laravel-mix with webpack config
- Can you provide plugins for swc HOT 1
- Can you provide a version based on swc core? HOT 1
- Uncaught ReferenceError: For is not defined HOT 5
- Not working with create-react-app HOT 7
- If is not defined HOT 4
- App breaks if i try to directly access the url - With Error - Choose is not defined HOT 1
- React-Native:Installation-Guide HOT 2
- last published version is 3 years old HOT 2
- ESLint autofix HOT 2
- Undefined is not a function with new JSX transform with react 17 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsx-control-statements.