Use a function that returns JSX to create a component.
Pass data using props to customize the component.
Examples
A simple button component that shows text from the label prop.
React
function Button(props) {
return <button>{props.label}</button>;
}
<Button label="Click me" />
A card component that shows a title and any nested content inside.
React
function Card({ title, children }) {
return (
<div className="card">
<h2>{title}</h2>
<div>{children}</div>
</div>
);
}
<Card title="Hello">This is inside the card.</Card>
Sample Program
This app shows two buttons using the same reusable Button component. Each button has its own label and click action. The buttons are accessible with aria-label and keyboard friendly.
A. The variable 'message' is not defined inside the component
B. The component does not receive props correctly
C. The usage of the component is incorrect
D. The component should return a button instead of a div
Solution
Step 1: Check how props are used inside the component
The component uses {message} but does not extract it from props.
Step 2: Identify the error
Since message is not defined or extracted, this causes a reference error.
Final Answer:
The variable 'message' is not defined inside the component -> Option A
Quick Check:
Props must be accessed via props or destructuring [OK]
Hint: Always get props from function parameter before use [OK]
Common Mistakes:
Using prop names directly without props or destructuring
Assuming usage syntax is wrong
Thinking component must return button
5. You want to create a reusable Input component that can show a label and accept user input. Which approach correctly allows customizing the label text and input placeholder using props?
hard
A. function Input() {
return (
<label>
label
<input placeholder="placeholder" />
</label>
);
}
B. function Input({ label, placeholder }) {
return (
<label>
{label}
<input placeholder={placeholder} />
</label>
);
}
C. function Input(props) {
return (
<label>
props.label
<input placeholder="props.placeholder" />
</label>
);
}
D. function Input({ label, placeholder }) {
return (
<label>
<input placeholder={label} />
{placeholder}
</label>
);
}
Solution
Step 1: Check prop usage for label and placeholder
Props should be destructured and used inside JSX expressions with curly braces.
Step 2: Verify correct placement of label and placeholder
function Input({ label, placeholder }) {
return (
<label>
{label}
<input placeholder={placeholder} />
</label>
);
} correctly places label text and uses placeholder prop inside input element.
Final Answer:
function Input({ label, placeholder }) {
return (
<label>
{label}
<input placeholder={placeholder} />
</label>
);
} -> Option B
Quick Check:
Destructure props and use inside JSX with curly braces [OK]
Hint: Destructure props and use inside JSX with curly braces [OK]
Common Mistakes:
Using prop names as plain text instead of expressions
Putting placeholder text in wrong prop
Mixing label and placeholder positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.