Earlier quoted context omitted.
From the article: “{number && } renders 0 instead of nothing. Use {number > 0 && } instead.” Functional JSX would look like: const isNumber = number > 0; {isNumber && } You can similarly do things like: const isVisible = condition1 && (condition2 || condition3) || guard(props.input1); {isVisible && } The functional paradigm and some basic code factoring can make quick work of conditional JSX
Yes, I'm well aware of how to use these. I will elaborate on why I think it's terrible (even though there is no alternative in standard JS yet): * It encourages JSX-specific idioms. Outside of JSX, using `&&` instead of `if` for control flow would raise eyebrows from most people, I think. * I find it easier and faster to refactor `if` statements to `if/else` and vice versa (only requires addition or deletion of code)…
Advice on JSX Conditionals
31–40 of 127 posts
Re: Advice on JSX Conditionals
#32Earlier quoted context omitted.
From the article: “{number && } renders 0 instead of nothing. Use {number > 0 && } instead.” Functional JSX would look like: const isNumber = number > 0; {isNumber && } You can similarly do things like: const isVisible = condition1 && (condition2 || condition3) || guard(props.input1); {isVisible && } The functional paradigm and some basic code factoring can make quick work of conditional JSX
Yes, I'm well aware of how to use these. I will elaborate on why I think it's terrible (even though there is no alternative in standard JS yet): * It encourages JSX-specific idioms. Outside of JSX, using `&&` instead of `if` for control flow would raise eyebrows from most people, I think. * I find it easier and faster to refactor `if` statements to `if/else` and vice versa (only requires addition or deletion of code)…
{do {
if (user) {
} else {
}
}}
https://babeljs.io/docs/en/babel-plugin-proposal-do-expressi...Re: Advice on JSX Conditionals
#33Earlier quoted context omitted.
I find ternaries alone to be totally adequate.
Oh they work, but stacking them tends to make future reading of the code harder due to their increased complexity.
It's a balance. Extracting logic into new components (and often new files!) isn't exactly ergonomic either.
I've found it's better to decrease readability if it makes understanding the logic more straight-forward (i.e. not hunting through several files to exhaust all possible outputs).
Re: Advice on JSX Conditionals
#34I think a better approach is to move the logic to a renderXxx() function: function renderInput(props:Props) { // Early exit if props are not as expected if (!props.cond1) return null; return ; } Then the parent markup is much cleaner, without any conditional: {renderInput(props)}
Not always worth it when you're just trying to do some conditional logic next to the code/components that it's related to.
Ideally we have the tools to decide when to add indirection ourselves instead being forced to do it to deal with complexity. You could also see this in callback-hell when we'd flatten callback trees with indirection—the tree looked flatter in the editor but we just moved code around. async/await gave us the tools to decide when we actually wanted it.
Re: Advice on JSX Conditionals
#35I've always found these approaches to writing conditions in JSX to be terrible. Wouldn't it be nice if JavaScript were an expression-oriented language? Then you could just write: {if (gallery.length) { }} There has been a "do expressions" proposal [0] for many years, which addresses this (though it is more verbose). I hope it will be accepted some day. [0] https://github.com/tc39/proposal-do-expressions
Re: Advice on JSX Conditionals
#36 if (isA) {
return A;
} else if (isB) {
return B;
} else if (isC) {
return C;
} else {
return D;
}
is equivalent to return isA ? A : isB ? B : isC ? C : D;Re: Advice on JSX Conditionals
#37Drives me nuts that there's no straightforward way to do a switch statement in JSX. {switch (page) { case 'home': return case 'about': return default: return }}
Sometimes an inline switch is indeed the clearest code. The closest JS has is the do-expression proposal: { do { switch... }}
Re: Advice on JSX Conditionals
#38Hard disagree on nested ternaries, or rather chained ternaries, which are just as easy to read and reason about as chained if else statements: if (isA) { return A; } else if (isB) { return B; } else if (isC) { return C; } else { return D; } is equivalent to return isA ? A : isB ? B : isC ? C : D;
Now throw in long functions with multiple arguments, and all kinds of different access patterns and this ternary starts to look like a nightmare.
Re: Advice on JSX Conditionals
#39I've always found these approaches to writing conditions in JSX to be terrible. Wouldn't it be nice if JavaScript were an expression-oriented language? Then you could just write: {if (gallery.length) { }} There has been a "do expressions" proposal [0] for many years, which addresses this (though it is more verbose). I hope it will be accepted some day. [0] https://github.com/tc39/proposal-do-expressions
Re: Advice on JSX Conditionals
#40I think a better approach is to move the logic to a renderXxx() function: function renderInput(props:Props) { // Early exit if props are not as expected if (!props.cond1) return null; return ; } Then the parent markup is much cleaner, without any conditional: {renderInput(props)}
function ConditionalComponent(props: Props) {
if (!props.condition) return null
return
}