4 ms·
Even with state, his code is overly verbose and can be expressed in a much more idiomatic way. var weAreConnected = Math.floor(Math.random() * 10) > 5;
by atoko 10y ago
Even with state, his code is overly verbose and can be expressed in a much more idiomatic way.
var weAreConnected = Math.floor(Math.random() * 10) > 5;
if (weAreConnected === true) {
this.setState({
isConnected: true
})
}
else {
this.setState({
isConnected: false
})
}
}
turns into
var weAreConnected = Math.floor(Math.random() * 10) > 5;
this.setState({
isConnected: weAreConnected
});
It's almost like he wrote it in the most obtuse way possible to prove his point. Unless he writes like that normally; If so then I can see where he's getting all these errors from.
- jontro 10y agoAbsolutely, especially since he/she wrote it that way in the swift version let weAreConnected: Bool = arc4random()%10 > 4 self.isConnected = weAreConnected
- deleted 10y ago[deleted]
- catshirt 10y ago"overly verbose" might be giving too much credit... it's bad code
- macspoofing 10y agoNo. 'Verbose' is correct. It's not bad code. It's a simple control flow.
- andrepd 10y agoUsing a branch when you can just do an assignment is objectively bad code.
- deleted 10y ago[deleted]
- macspoofing 10y agoObjectively eh? And bad code by what criteria? In this case, it's a little more verbose than it needs to be, but hurts nothing.
- mirekrusin 10y agoBut violates DRY = bad code. Not incorrect because it does what it's suppose to, just in a bad way.
- faceyspacey 10y agoIt didn't need to happen in componentDidMount with multiple calls to setState. If it's not getting info that it would only know after mounting, it could have happened in the same line that isConnected is initially defined within the constructor!
- faceyspacey 10y agoconstructor() { super() this.state = { isConnected: Math.floor(Math.random() * 10) > 5 } }
- tracker1 10y agoFor that matter, FB encourages the use of unidirectional workflows and simpler components... which means it could have been a pure function receiving props.