Forked from coryhouse/callingChildComponentInRender.js
Last active
August 7, 2018 15:11
-
-
Save vamsiampolu/6a7b3d28d6242a8540779ef79a4f73f6 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 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) | |
} | |
}) | |
} | |
renderUserList = () => { | |
const {users} = this.state | |
return users.map(user => { | |
return (<UserListItem | |
key={user.id} | |
user={user} | |
onClick={this.deleteUser} | |
/>) | |
}) | |
} | |
render() { | |
return ( | |
<div> | |
<h1>Users</h1> | |
<ul> | |
{this.renderUserList()} | |
</ul> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment