News for hackers. Stuff that matters.
Re:What should go into JSX 2.0? by maaaats on Sunday October 16, 2016 @11:53AM (#12719155)
I like how the if proposal is down voted, probably because it's mentioning Angular. Conditional rendering in React is not always very clean. Most codebases end up resorting with something similar to this in a container to show a spinner while data is loading and rendering it when done.

<ConditionalSpinner renderIf={data}><PersonRenderer person={data.person} /></ConditionalSpinner>

Problem is that the inner component will still be parsed and fail with cannot read property person of undefined. Yes, you can do this with a simple condition in your render function, but then it's not declarative any more.

Re:What should go into JSX 2.0? by woah on Sunday October 16, 2016 @02:39PM (#12720178)
You think it's downvoted because it mentions Angular? Baseless complaining. JSX is a breath of fresh air after all the templating languages that force you to learn a crippled pseudolang. What's next? For loops? Moving every feature that already exists in JavaScript into JSX with some arbitrary additional syntax? What's the point? It's already very easy to do conditionals with && or ternaries.
Re:What should go into JSX 2.0? by biscarch on Sunday October 16, 2016 @01:30PM (#12719739)
Loading (and error, etc) state should be the responsibility of the component doing the content rendering, not the parent. So the ConditionalSpinner component:

   <ConditionalSpinner renderIf={data}><PersonRenderer person={data.person} /></ConditionalSpinner>
Could be written better as:

   <PersonRenderer person={data.person} />
Where PersonRenderer would be defined as such:

   class PersonRenderer extends Component {
     render() {
       if(this.props.person) {
         return <div>personcontent</div>
       }
       return <LoadingIndicator/>
     }
   }
Given this, I'm still not sure how an If "component" would be achieved (and further, you'd need an Else or a Switch component anyway if going down this route). Maybe you could explain the benefits of such a path?
Re:What should go into JSX 2.0? by snappy173 on Sunday October 16, 2016 @01:57PM (#12719918)
or create a wrapper function ...

  function withLoadingIndicator(Wrapped) {
    return function(props) {
      if (props.loading) {
        return (<LoadingIndicator />);
      }
      return (<Wrapped {...props} />);
    }
  }

  // ...

  export class PersonRenderer extends Component {
    render() {
      return <div>personcontent</div>
    }
  }

  export default withLoadingIndicator(PersonRenderer);
Re:What should go into JSX 2.0? by leesalminen on Sunday October 16, 2016 @05:31PM (#12721102)
I first learned about this method with react-dnd and have used it extensively since. Quite elegant.
Re:What should go into JSX 2.0? by maaaats on Sunday October 16, 2016 @01:40PM (#12719810)
> Loading (and error, etc) state should be the responsibility of the component doing the content rendering

It's often divided in two: One container fetching the data, and a pure component rendering it.

Re:What should go into JSX 2.0? by biscarch on Sunday October 16, 2016 @02:21PM (#12720052)
I think my intent might have come across wrong here.

The "states" aren't related to container/dumb components or data-fetching/selection logic. They're purely about rendering the state for a given segment of the page. Perhaps this article[0] might help convey my intent. It's more about how to render "we didn't get data from that one endpoint for this section of the page" or "we're waiting for the data to show up", etc.

[0]: https://medium.com/swlh/the-nine-states-of-design-5bfe9b3d6d...

Re:What should go into JSX 2.0? by doque on Sunday October 16, 2016 @12:00PM (#12719194)
How about { data && <Inner person={ data.person }/> } ?
Re:What should go into JSX 2.0? by ponyous on Sunday October 16, 2016 @12:28PM (#12719384)
If you render a more complex component this introduces another level of indentation. I can see why some people would prefer cleaner code but in my opinion, indenting ifs is a good thing as it increases the readability and allows you to spot if statements faster.
Re:What should go into JSX 2.0? by zackify on Sunday October 16, 2016 @01:06PM (#12719612)
this is why you should be using immutable js. It will not throw errors accessing an object.
Re:What should go into JSX 2.0? by hdra on Sunday October 16, 2016 @10:30PM (#12722351)
I down voted that proposal as well, but not because the mention of Angular, but because I don't like the `ng-if` way of doing conditionals. One of the reason I like JSX is because it is "just Javascript". Putting the flow control logic into component attributes like that is a terrible idea for JSX.
Re:What should go into JSX 2.0? by spdustin on Sunday October 16, 2016 @02:12PM (#12719998)
JSX shouldn't be used to turn a component into a controller for child components. That's why I wouldn't want that kind of logic in there.
Re:What should go into JSX 2.0? by inimino on Sunday October 16, 2016 @09:48PM (#12722184)
In what sense is it not declarative anymore?

I usually use tests at the top of the render function to make sure that data is there, and return cleanly if it isn't. It just makes development easier as React components tend to get re-used. Within the data.person, in your example, the caller should make sure that it is complete, or pass nothing at all.

In the case you show I would either return <Spinner/> or the content, but never nest them. Have a conditional and two return statements, or one return statement with a ternary operator.

Re:What should go into JSX 2.0? by buchanaf on Monday October 17, 2016 @12:27AM (#12722694)
I actually generally include this in my Babel configuration: https://www.npmjs.com/package/jsx-control-statements.

People might argue that this isn't the React way of doing things, but it's easier than setting a conditional classname to display none. It's also easier than breaking out a small amount of html into a separate component just because you want some conditional display logic.

Re:What should go into JSX 2.0? by Marazan on Sunday October 16, 2016 @01:27PM (#12719719)
The Flex method of conditional rendering is nice, far better than ng-if all over the place.
Re:What should go into JSX 2.0? by coderzach on Sunday October 16, 2016 @12:38PM (#12719438)
Now that sweet.js supports babel, the best option may be to write an if expression macro.
Re:What should go into JSX 2.0? by tracker1 on Sunday October 16, 2016 @04:14PM (#12720687)
You can already do that...

    function ConditionalSpinner({renderIf, children}) {
      if (!renderIf) return null;
      return children[0];
    }
Re:What should go into JSX 2.0? by msoad on Sunday October 16, 2016 @12:17PM (#12719310)
It's the extreme fanboyism there! Wrapping every component that has `if` attribute with an `if` statement should not result into that:

    <div *if={cond1 && cond2}>
        <div>foo</div>
    </div>

    // results to
    if (cond1 && cond2) {
        return React.createElement('div', null, 
            React.createElement('div', null, 'foo')
        );
    }
I'm not sure why this is bad? Because it's looking like Angular?!!
Re:What should go into JSX 2.0? by coderzach on Sunday October 16, 2016 @12:47PM (#12719499)
It's bad because it's inconsistent with how every other attribute in jsx works. Nothing else in jsx changes the control flow of the code.
Re:What should go into JSX 2.0? by msoad on Sunday October 16, 2016 @12:49PM (#12719514)
It is 2.0 so it can have breaking changes
Re:What should go into JSX 2.0? by exogen on Sunday October 16, 2016 @02:59PM (#12720309)
The parent didn't mention anything about backwards compatibility. They're saying 2.0 with such a change would be inconsistent with itself. And they're right.
Re:What should go into JSX 2.0? by cel1ne on Sunday October 16, 2016 @12:23PM (#12719348)
I think ternary expression are rather ok here, though not perfect:

    {cond1 && cond2
     ? <div>foo</div>
     : null
    }
Re:What should go into JSX 2.0? by danneu on Sunday October 16, 2016 @12:54PM (#12719543)
And you can inline if-statements with an IIFE when you need a little more space:

    {(() => {
      if (cond1 && cond2)
        return <div>foo</div>
      else 
        ...
    })()}
Though it makes GitHub think it's a Clojure file.
Re:What should go into JSX 2.0? by vanderZwan on Sunday October 16, 2016 @12:58PM (#12719570)

     {(() => {
     })()}
Honestly I can't decide if I think this is ugly or beautiful. It's like the buffalo buffalo of JS.
Re:What should go into JSX 2.0? by olalonde on Sunday October 16, 2016 @05:32PM (#12721105)
That's something the "do expressions" ECMAScript proposal is meant to solve:

    {do {
      if (cond1 && cond2)
        <div>foo</div>
      else 
        ...
    }}
If JSX decides to implement "implicit" "do expressions", I suppose it would just look like this:

    {
      if (cond1 && cond2)
        <div>foo</div>
      else 
        ...
    }
http://wiki.ecmascript.org/doku.php?id=strawman:do_expressio...
Re:What should go into JSX 2.0? by spirit23 on Sunday October 16, 2016 @01:25PM (#12719702)
It’s interesting to see that there are so many ways of doing conditions in JSX. I myself wrote a tiny library (http://github.com/yuanchuan/cond) for it several months ago and thought it made the code looks clean but suffers the same undefined issue though.
Re:What should go into JSX 2.0? by _betty_ on Sunday October 16, 2016 @02:51PM (#12720259)
I'd love for them to adopt something similar to Razor (Microsoft server side html engine)

note: i've changed your example to show code inside html tags, i'm aware it does something different to yours.

conditionals

  <div>
    @if(cond1 && cond2) {
      <div>foo</div>
    }
  </div>

loops

  <div>
    @for(var i in collection) {
      <div></div>
    }
  </div>

variables

  <div>
    @variable
  </div>

ternary/longer statement

  <div>
    @(variable ? 1 : 2)
  </div>
Re:What should go into JSX 2.0? by tmzt on Sunday October 16, 2016 @07:30PM (#12721684)
The one negative thing I've found with Razor is the ambiguous start and end of statements or expressions vs html. Sometimes a @ is required before a statement and sometimes its a syntax error, conversely statements and and expressions may be rendered as text if they follow html with a @. I think jsx's approach is clearer.
Re:What should go into JSX 2.0? by _betty_ on Monday October 17, 2016 @09:23PM (#12731283)
admittedly there is the occasional moment I hit where I need an extra @ or @: so it's not perfect, but I have much less issue reading that format than the { true && <element /> } syntax, also think @if is a lot nicer than <if>.

Maybe just a personal thing.

Re:What should go into JSX 2.0? by Yaggo on Sunday October 16, 2016 @01:02PM (#12719588)
<div if={expression} />

translates to

React.createElement('div', {if: <expression>})

This is bad because createElement always gets execute, whether the if expression evaluated to true or false. Changing this semantic would be messy.