News for hackers. Stuff that matters.
Re:Why I'm not a React Native developer by vmarsy on Saturday August 19, 2017 @03:33PM (#15055134)
What if `weAreConnected` is `null` or `undefined`?

WalterSear's solution solves the issue since both `null` and `undefined` would become `false`:

    this.setState({
        isConnected: Boolean(weAreConnected)
    })

but in your case you could set `isConnected` to `null` or `undefined`

so if later in code someone makes the mistake of writing:

    if (this.state.isConnected === false) { /* do stuff */ }
they could have a bad surprise.
Re:Why I'm not a React Native developer by debaserab2 on Saturday August 19, 2017 @03:40PM (#15055166)
this.setState({ isConnected: !!weAreConnected })
Re:Why I'm not a React Native developer by hypervis0r on Saturday August 19, 2017 @03:46PM (#15055207)
This^, plus, you shouldn't really have "null" or "undefined" variables which you treat as bool (if you do, then that's a bug)
Re:Why I'm not a React Native developer by DeonPenny on Saturday August 19, 2017 @07:37PM (#15056218)
Ding ding ding right answer. This is the way most senior javascript devs would right it.
Re:Why I'm not a React Native developer by corn13read on Sunday August 20, 2017 @03:40AM (#15057381)
They'd right it if they were fixing it or write it if they were writing it ;)
Re:Why I'm not a React Native developer by Rockslide on Saturday August 19, 2017 @03:41PM (#15055176)
It certainly doesn't hurt to "cast" the input to boolean. But your second example is the next code smell, because it should just be

  if (!this.state.isConnected) { /* do stuff */ }
(which still works as expected even when dealing with null or undefined).
Re:Why I'm not a React Native developer by vmarsy on Saturday August 19, 2017 @03:50PM (#15055224)
Agreed, but you can't anticipate what another dev could write :)

Or if you pass down that `isConnected` down to a library where for some reason they assumed `null` should default to `true`

Of course it's very unlikely, but my point is you can't know all use cases of `this.state.isConnected` in advance on a large project so as you say it doesn't hurt to sanitize the input.

Re:Why I'm not a React Native developer by mikeycgto on Saturday August 19, 2017 @05:48PM (#15055810)
That's why there's PropTypes so you can define what type a given prop should be. An error is raised if a higher level component passes in a prop of an incorrect type.
Re:Why I'm not a React Native developer by maxxxxx on Saturday August 19, 2017 @04:04PM (#15055289)
this.setState({ isConnected: (weAreConnected === true) })

seems 100% equivalent

Re:Why I'm not a React Native developer by xamuel on Sunday August 20, 2017 @01:10AM (#15057039)
Another flavor of this surprise: JSON.stringify doesn't "see" undefined. JSON.stringify({foo:undefined}) returns "{}" :)
Re:Why I'm not a React Native developer by oceanswave on Sunday August 20, 2017 @07:38PM (#15060837)
Because type (prototype) and data (Json) are two different things, if you need it -- JSON.stringify(obj, (k, v) => v === undefined ? null : v);