Live data from Hacker News

Advice on JSX Conditionals

thoughtspile.github.io

61–70 of 127 posts

Re: Advice on JSX Conditionals

#61

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

Control-flow components like ""... because you couldn't stand just writing "for" or "if" like a caveman and had to invent a custom DSL for it.

Re: Advice on JSX Conditionals

#62
post #47

Earlier quoted context omitted.

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.

Try as I might, I have to reason through ternaries every time I encounter them. I think the = being so removed from what it's actually assigning, but without grouping parens, is what messes me up. Plus I always forget what the punctuation characters mean—if/elseif/else uses words, so I don't have to remember.

Just read whatever has a question mark after it with the intonation of a question, and it flows naturally.

Re: Advice on JSX Conditionals

#64

Earlier quoted context omitted.

The example you put forward could be resolved through using a router. The router implementations are essentially fancy switches without ”switch”.

yeah, I use reach/router for this kind of thing. A page switcher was just the first example that popped into my head. Here's a real world example if it matters… const Grid = (gridItems) => ( {gridItems.map(item => ( _} )} const GridItem = (props) => { switch(props._type) { case 'image': return case 'video': return case 'copy': return // slideshows, 3D stuff, newsletter signup forms… default: return } } … and I suppos…

I'd write it like this

  const gridComponent = {
      image: GridImage,
      video: GridVideo,
      copy: GridCopy
  }

  const Grid = (gridItems) => (
    
      {gridItems.map(item => {
        const GridComponent = gridComponent[item._type]
        return GridComponent ?  : null
      }}
    
  )}

Re: Advice on JSX Conditionals

#65
post #25

Earlier quoted context omitted.

Another quick option for avoiding the "zero" issue: {!!number && }

Better to use Boolean(number) Its more obvious what it achieves just at a glance.

"!!" should be pretty obvious to any javascript developer.

Re: Advice on JSX Conditionals

#66

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 ; } })()}

[deleted]

Re: Advice on JSX Conditionals

#67
post #7

I've always much preferred using local variables to hold conditional fragments. Since React will safely ignore null/undefined, you can do: let child; if (someCondition) { child = ; } return ( Maybe here's a child: {child} ); This lets you avoid embedding conditional logic in your (already pretty dense) JSX tree.

I do this sometimes but having to look up where child was defined and everything that could have changed it can be tiresome.

That's why a function can be better, at least you know that the value isn't changed somewhere unexpected.

Re: Advice on JSX Conditionals

#68
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;

Especially if you format them well. Just use a newline before each `:` and maybe after each `?` (if the value expressions are large), and it'll look great.

  return isA ? A
    : isB ? B
    : isC ? C
    : D

Re: Advice on JSX Conditionals

#69

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

Control-flow components like " "... because you couldn't stand just writing "for" or "if" like a caveman and had to invent a custom DSL for it.

Solid actually uses them for static analysis which ~isn’t otherwise possible in JSX, which has to be an expression. It would be great if for/if were expressions in JS, but they’re not.

Re: Advice on JSX Conditionals

#70

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

I mean, sure. But compilers often impose complexity as a trade off for performance or other benefits.
Post reply on HN