Use one component twice with different props. Follow React as it calls the same function once for each use.
React · beginner · 6 min · 9 steps · free
function Greeting({ name, emoji }) {
const message = `Hello, ${name}!`;
return (
<p>
{emoji} {message}
</p>
);
}
export default function App() {
return (
<div>
<Greeting name="Ada" emoji="👋" />
<Greeting name="Linus" emoji="🐧" />
</div>
);
}App, the top component.App returns JSX that uses Greeting twice, with different attributes. For a component, these attributes are called props.Greeting with the props from the JSX: name = "Ada", emoji = "👋". The { name, emoji } in the parameter list unpacks them.Greeting is plain JavaScript: message = "Hello, Ada!".Greeting returns JSX: a description of what the screen should show, <p>👋 Hello, Ada!</p>. React has not changed the page yet.<Greeting>, now with name = "Linus". Each use of a component is its own call, with its own props.message = "Hello, Linus!".Greeting returns JSX: a description of what the screen should show, <p>🐧 Hello, Linus!</p>. React has not changed the page yet.Answer: { name: "Ada" }. Props are one object.
Answer: No, props are read-only. Only the parent decides its props.
← JSX: Markup Inside JavaScript · Composition with children →