Earlier quoted context omitted.
Sure, when you're neatly using booleans with names that are three characters long, this is super easy to parse. Now throw in long functions with multiple arguments, and all kinds of different access patterns and this ternary starts to look like a nightmare.
It's still often the cleanest way and I find myself doing something like this: let x = somelongfunc(arg, blah(etc)) ? value1 : some other condition ? value2 : fallback What I usually want is pattern matching expressions, but those are not in many languages.
Advice on JSX Conditionals
121–127 of 127 posts
Re: Advice on JSX Conditionals
#122IMHO this is an anti-pattern. This involves people writing giant blocks of JSX that are hard to read. Especially with && all over the place. Just split up your component into smaller components with max one conditional each. Related, can anyone tell me the origins of the bizarre react practice of one file per component? I'm assume some mega corp told people it was "best practice", but the end result is way too many f…
Re: Advice on JSX Conditionals
#123Earlier quoted context omitted.
I'm sorry, but you're just mistaken about how JSX works. does not translate into a call to Foo(). It translates into React.createElement(Foo). And Foo will only be called if the element returned from that call winds up being examined by some renderer like ReactDOM, mounted as a component, and actually rendered. Here: function UnrenderedComponent() { console.log("UnrenderedComponent does not get executed"); React.useE…
> I'm sorry, but you're just mistaken about how JSX works. No, I’m not. JSX doesn’t do anything. But it’s an expression and evaluated according to the rules of JS expressions. You might expect React or whatever to evaluate it one way today, and it can be evaluated another way tomorrow. React.createElement isn’t the API you’re using, neither is react/jsx-runtime. You’re using an expression with some special angle brac…
But you are, I think, retreating to a position that, because the JavaScript && operator shortcircuits (ie does not evaluate the second operand of the first operand is truthy) it is always a control flow - a point which I can sort of agree with, but which I think is just a weak position, since it doesn’t help us make decisions about what code is good or bad.
Because if we back up to the top of the thread the point being raised was that use of && for conditionals in react feels like using && to perform control flow, and that is idiomatically bad JS.
But you can’t have it both ways - if using && for control flow is bad and && is always a control flow operation then every use of it is bad. But if there are some times where && use is good, then there must be cases where using &&, even though it shortcircuits, does not count as using it for evil control flow.
But even in simple idiomatic JavaScript we rely on && shortcircuiting for good and valuable purposes like nullsafety:
if (person !== null && person.name === ‘foo’)
So is that using && for control flow, which is bad?What about here?
return person && person.name;
Or return person && `Name: ${person.name}`;
.. or even return person &&
(Notice what I’m guarding against here is not against incorrect evaluation of the Person control - I’m guarding against incorrect evaluation of one of its parameters)My position is, && shortcircuiting is a feature and you can use it as such, but if your code relies in a nonobvious way on the shortcircuiting behavior for correctness, then you are using && as a control flow tool, and you have made it brittle, harder to refactor, and harder to parse.
But if your code would still be correct even if JavaScript did not guarantee that it would not evaluate the second parameter if the first was truthy, then you’re not even relying on shortcircuiting, and using && is clean and valid.
And since JSX literals in react are really just shorthand for a special kind of object literal, and in general therefore it doesn’t matter if they are evaluated or not, I take the position that using them within JS && expressions is legitimate, and not an abuse of && to perform complex control flow.
Re: Advice on JSX Conditionals
#124Hard 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;
Maybe, but sonarqube doesn't like it, which means I can't pass the CI pipeline, so...
Re: Advice on JSX Conditionals
#125Earlier quoted context omitted.
Funnily enough, this has existed in React through a Babel plugin. I'm not sure why this hasn't really caught on. https://github.com/AlexGilleran/jsx-control-statements
I’ve encountered that, and kind of don’t understand why you’d need a Babel plugin. They’re trivial components to implement in regular JSX.
Re: Advice on JSX Conditionals
#126Earlier quoted context omitted.
> I'm sorry, but you're just mistaken about how JSX works. No, I’m not. JSX doesn’t do anything. But it’s an expression and evaluated according to the rules of JS expressions. You might expect React or whatever to evaluate it one way today, and it can be evaluated another way tomorrow. React.createElement isn’t the API you’re using, neither is react/jsx-runtime. You’re using an expression with some special angle brac…
You said that “evaluating a component when you don’t intend to use it” will do a bunch of things which React manifestly doesn’t do . I shared a link to some code you can run that demonstrates this. The question of whether or not some code gets executed is, as you said, knowable . But you are, I think, retreating to a position that, because the JavaScript && operator shortcircuits (ie does not evaluate the second oper…
I’m not retreating to anything. This was my only point and I explicitly said so.
> a point which I can sort of agree with, but which I think is just a weak position, since it doesn’t help us make decisions about what code is good or bad.
I’m not trying to change the world with the point. Just to establish the basic fact.
Re: Advice on JSX Conditionals
#127Hard 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;
return isA && A || isB && B || isC && C || D;
Am I weird?