Created
May 1, 2019 19:03
-
-
Save jlittlejohn/5d5cfed4adc60296b6d9ad45ba1f3880 to your computer and use it in GitHub Desktop.
REACT: Render an Array of Objects in a Class Component
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
var users = [ | |
{ id: 1, name: "Chris" }, | |
{ id: 2, name: "Jeff" }, | |
{ id: 3, name: "Julie" }, | |
{ id: 4, name: "Jessica" } | |
]; | |
class UserList extends React.Component { | |
render() { | |
var users = this.props.users; | |
var usersListItems = users.map(function(user) { | |
return <div key={user.id}>{user.name}</div>; | |
}); | |
return <div>{usersListItems}</div>; | |
} | |
} | |
// or | |
// class UserList extends React.Component { | |
// render() { | |
// var users = this.props.users; | |
// return ( | |
// <div> | |
// {users.map(function(user) { | |
// return <div key={user.id}>{user.name}</div>; | |
// })} | |
// </div> | |
// ); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment