프로그래밍/Web
Mapping Components
studylida
2024. 4. 29. 21:00
import React from "react";
import Card from "./Card";
import contacts from "../contacts";
function createCard(contact) {
return (
<Card
key={contact.id}
name={contact.name}
img={contact.imgURL}
tel={contact.phone}
email={contact.email}
/>
);
}
function App() {
return (
<div>
<h1 className="heading">My Contacts</h1>
{contacts.map(createCard)}
{/* <Card
name={contacts[0].name}
img={contacts[0].imgURL}
tel={contacts[0].phone}
email={contacts[0].email}
/>
<Card
name={contacts[1].name}
img={contacts[1].imgURL}
tel={contacts[1].phone}
email={contacts[1].email}
/>
<Card
name={contacts[2].name}
img={contacts[2].imgURL}
tel={contacts[2].phone}
email={contacts[2].email}
/> */}
</div>
);
}
export default App;
another example
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
ReactDOM.render(<App />, document.getElementById("root"));
emojipedia.js
const emojipedia = [
{
id: 1,
emoji: "💪",
name: "Tense Biceps",
meaning:
"“You can do that!” or “I feel strong!” Arm with tense biceps. Also used in connection with doing sports, e.g. at the gym."
},
{
id: 2,
emoji: "🙏",
name: "Person With Folded Hands",
meaning:
"Two hands pressed together. Is currently very introverted, saying a prayer, or hoping for enlightenment. Is also used as a “high five” or to say thank you."
},
{
id: 3,
emoji: "🤣",
name: "Rolling On The Floor, Laughing",
meaning:
"This is funny! A smiley face, rolling on the floor, laughing. The face is laughing boundlessly. The emoji version of “rofl“. Stands for „rolling on the floor, laughing“."
},
{
id: 4,
emoji: "🤓",
name: "Nerd Face",
meaning:
"Huge glasses, awkward smile and buck teeth. Used humorously or ironically for nerds or to express how smart you are. Stereotype of a nerd; a smart but funny-dressed person with social deficits."
}
];
export default emojipedia;
App.jsx
import React from "react";
import Entry from "./Entry";
import emojipedia from "../emojipedia";
//1. Extract the repeated parts of the App into a Entry component.
//2. Use props to make the Entry component render different data.
//3a. Import the emojipedia constant.
//3b. Map through the emojipedia array and render Entry components.
//Emojipedia has 3 entries, so createEntry gets called 3 times.
//Each time, it passes 1 item from the emojipedia array as a var called emojiTerm.
//var emojiTerm = {
// id: 1,
// emoji: "💪",
// name: "Tense Biceps",
// meaning:
// "“You can do that!” or “I feel strong!” Arm with tense biceps. Also used in connection with doing sports, e.g. at the gym."
// }
function createEntry(emojiTerm) {
return (
<Entry
key={emojiTerm.id}
emoji={emojiTerm.emoji}
name={emojiTerm.name}
description={emojiTerm.meaning}
/>
);
}
function App() {
return (
<div>
<h1>
<span>emojipedia</span>
</h1>
<dl className="dictionary">{emojipedia.map(createEntry)}</dl>
</div>
);
}
export default App;
Entry.jsx
import React from "react";
function Entry(props) {
return (
<div className="term">
<dt>
<span className="emoji" role="img" aria-label="Tense Biceps">
{props.emoji}
</span>
<span>{props.name}</span>
</dt>
<dd>{props.description}</dd>
</div>
);
}
export default Entry;