Last active
July 7, 2017 07:48
-
-
Save coryhouse/f5758f5355da5280fc0e3350453fef31 to your computer and use it in GitHub Desktop.
Example of calling extracted child component in render to avoid binding or declaring an arrow function
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
import React from 'react'; | |
import UserListItem from './UserListItem'; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
users: [ | |
{ id: 1, name: 'Cory' }, | |
{ id: 2, name: 'Meg' } | |
] | |
}; | |
} | |
deleteUser = id => { | |
this.setState(prevState => { | |
return { | |
users: prevState.users.filter( user => user.id !== id) | |
} | |
}) | |
} | |
render() { | |
return ( | |
<div> | |
<h1>Users</h1> | |
<ul> | |
{ | |
this.state.users.map( user => { | |
return ( | |
<UserListItem | |
key={user.id} | |
user={user} | |
onClick={this.deleteUser} | |
/> | |
) | |
}); | |
} | |
</ul> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any particular reason for using
over this shorter format?
Same goes for the map:
this.state.users.map( user => { return (<UserListItem...) } )
vsthis.state.users.map( user => (<UserListItem...) )
I always feel better when I can omit a return and a couple parenthesis.. 😄