Two Props
Part of the Fundamentals section of Coddy's React journey. Lesson 18 of 42.
Components routinely take several props. List them all in the destructuring and mix them into the JSX:
function Product({ name, price }) {
return <p>{name} costs ${price}</p>;
}
<Product name="Laptop" price={999} />
// renders: Laptop costs $999Notice the types: name is a string (quotes), price is a number (braces). Keeping numbers as numbers matters the moment you calculate with them.
Challenge
EasyBuild a product line for the shop.
- Make
Productreceivenameandpriceprops and render{name} costs ${price}in its paragraph. - In App, render one Product with name
Laptopand price999(as a number!). The page should showLaptop costs $999.
Try it yourself
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script type="module" src="main.jsx"></script>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle