Skip to content

Instantly share code, notes, and snippets.

@aerrity
Created December 12, 2018 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aerrity/2844a31192268100485eede6bea4a7b6 to your computer and use it in GitHub Desktop.
Save aerrity/2844a31192268100485eede6bea4a7b6 to your computer and use it in GitHub Desktop.
Unstyled version of User cards example
import React from "react";
import ReactDOM from "react-dom";
// This loads data from a file named 'data.json' in the same directory
import data from "./data";
// Store the array of user objects in a variable
const users = data.results; // [{user},{user}, ...]
// Component to represent a single User 'Card' (note: this is a function component so cannot use state)
function User(props) {
return (
<div>
<img alt="Profile" src={props.image} />
<h2>{props.name}</h2>
<p>{props.quote}</p>
</div>
);
}
// Iterate across all elements (u) in the users array, producing a User component for each
// Data is passed into the components via props
const userList = users.map(u => (
<User
key={u.name.first}
name={u.name.first}
image={u.picture.medium}
quote={u.quote}
/>
));
// Render all the User components in a 'grid' layout
ReactDOM.render(<div>{userList}</div>, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment