Last active
November 17, 2016 17:10
-
-
Save slorber/27447ab86ac61ddb229d4108e521c861 to your computer and use it in GitHub Desktop.
Use do { } makes React render() method more readable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FP languages permit to evaluate if/else expressions as values. | |
// In many cases it permits to avoid using mutable variables (var/let), multiple returns, or nested ternary operators | |
// This is the do { } notation (stage 0), that permits to write the following right now with Babel. | |
const Users = ({users}) => ( | |
<div> | |
{users.map(user => | |
<UserCard key={user.id} user={user}/> | |
)} | |
</div> | |
) | |
const UserList = ({users}) => do { | |
if (!users) <div>Loading</div> | |
else if (!users.length) <div>Empty</div> | |
else <Users users={users}/> | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@gre I agree with you and often use code-blocks in scala to limit the scope of unnecessary variables.
I'm not sure it can work with switch because in switch it's not like pattern matching, and you can enter on multiple "branches" if you don't use break/return. Inside
do {}
, if you don't use return on switch it... does not return anything. If you put a return it says you are not in a function context so can't return.I guess what you want is this in a more idiomatic way? I'd also love that
Isn't it annoying to have to use
let
reassignment with switch, or this weird function syntax ? :)