react-cardlist
Goals
Copying and pasting cards is tedious, so we create a
CardListcomponentHard-code the data as an array in
robots.jsto start with
export const robots = [
{
id: 1,
name: "Leanne Graham",
username: "Bret",
email: "Sincere@april.biz"
},
...
}Connecting the Pieces
Then pass that array to
CardListinindex.js
ReactDOM.render(
<div>
<CardList robots={robots} />
</div>,
document.getElementById("root")
);The Card List
CardListtransforms the array of values into an array of views usingmapWraps that array in a
divbecause it can only return one thingJSX automatically concatenates array elements for us
const CardList = ({ robots }) => {
const cardsArray = robots.map(robot => (
<Card
name={robot.name}
email={robot.email}
id={robot.id} />
));
return (
<div>
{cardsArray}
</div>
);
};PropTypes
Tell React that the
robotsarray is required
CardList.propTypes = {
robots: React.PropTypes.array.isRequired
};Last updated
Was this helpful?