Live data from Hacker News

Advice on JSX Conditionals

thoughtspile.github.io

51–60 of 127 posts

Re: Advice on JSX Conditionals

#51
post #50

Earlier quoted context omitted.

You can add do-expression proposal support in .babelrc: {do { if (user) { } else { } }} https://babeljs.io/docs/en/babel-plugin-proposal-do-expressi...

Or, no need to enable anything: {user ? : }

Ok this thread just reached peak JavaScript.

Re: Advice on JSX Conditionals

#52

When logic gets more complicated than a single ternary (e.g., handling pending/error/result state), I've found that a `useMemo` gives me the flexibility I need. It also keeps the messy logic out of the final JSX, which tends to make it read better.

I've personally rarely even needed to useMemo, I just write a function that returns the JSX I need and { myFunction() } in the functional component's return (given React).

If you're using Typescript that can get a bit annoying, since you'll also need to pass in whatever props necessary for the function to handle the logic + type them. Makes it nice and easy to test in isolation though.

Re: Advice on JSX Conditionals

#54

It doesn’t necessarily have the same benefits in React, but folks ought to consider using components for control flow, like SolidJS does[1]. 1: https://www.solidjs.com/docs/latest/api#control-flow

Some people consider this added complexity?

https://news.ycombinator.com/item?id=30509806

Re: Advice on JSX Conditionals

#56

Earlier quoted context omitted.

I've personally rarely even needed to useMemo, I just write a function that returns the JSX I need and { myFunction() } in the functional component's return (given React).

If you're using Typescript that can get a bit annoying, since you'll also need to pass in whatever props necessary for the function to handle the logic + type them. Makes it nice and easy to test in isolation though.

Typically the function has access to whatever props or state it needs because it's in the scope of the component. It's not pure, but it's rare that I need to verbosely pass props into the function call and then accept them in myFunction. I don't typically test these functions in isolation as the component itself is pure and I test the output of the component as a whole instead.

Re: Advice on JSX Conditionals

#58
post #3

I'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

For these situations I often use IFFEs with if-statements and early returns inside.

Re: Advice on JSX Conditionals

#59
post #36

Hard 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;

I have been writing code in C-like languages since 1996 and I find the ternary equivalent to be unreadable.

If you're ever doing this in any language with simple if() conditions, you need to refactor anyway.

Re: Advice on JSX Conditionals

#60
protip: you can use an IIFE (immediately invoked function expression) to put whatever conditionals or logic you want inside JSX! I use this all the time and it's much nicer for more complex blocks:

    {(() => {
      switch(state) {
        case 'loading': return ;
        case 'ready': return ;
        case 'error': return ;
      }
    })()}
Post reply on HN