Discover how a tiny change in your code can save hours of repetitive work!
Why Using props in JSX in React? - Purpose & Use Cases
Imagine you want to show a list of friends on a webpage, and for each friend, you write separate HTML code with their name and picture manually.
Writing separate HTML for each friend is slow, repetitive, and if you want to change the layout, you must update every single place manually, which is easy to forget or mess up.
Using props in JSX lets you create one reusable component that takes different data (props) and shows it correctly, so you write less code and can update the design in one place.
<Friend name="Alice" picture="alice.jpg" /> <Friend name="Bob" picture="bob.jpg" /> <Friend name="Carol" picture="carol.jpg" />
const friends = [{name: 'Alice', picture: 'alice.jpg'}, {name: 'Bob', picture: 'bob.jpg'}, {name: 'Carol', picture: 'carol.jpg'}];
friends.map(f => <Friend key={f.name} name={f.name} picture={f.picture} />)You can build flexible, reusable UI pieces that adapt to different data easily and keep your code clean and simple.
Think of a social media app showing posts: each post looks the same but shows different text and images. Props let the app reuse the post layout for every user's content.
Manual HTML for each item is repetitive and error-prone.
Props let components receive data to display dynamically.
This makes UI code reusable, easier to maintain, and scalable.
Practice
props in React components?Solution
Step 1: Understand the role of props
Props are used to send data from a parent component to a child component in React.Step 2: Differentiate props from other concepts
State stores internal data, events handle user actions, and CSS styles components, but props specifically pass data down.Final Answer:
To pass data from a parent component to a child component -> Option DQuick Check:
Props = Data passing [OK]
- Confusing props with state
- Thinking props handle events
- Believing props style components
title inside JSX?Solution
Step 1: Use curly braces to embed prop values in JSX
In JSX, to show a prop value, you wrap the prop access in curly braces like{props.title}.Step 2: Understand context of prop usage
Thepropsobject is passed to the component function, so access via{props.title}. Using{title}requires destructuring likefunction Comp({title}).Final Answer:
<h1>{props.title}</h1> -> Option CQuick Check:
Curly braces show prop values [OK]
- Writing prop name as plain text without braces
- Using $ sign like in other languages
- Using {title} without destructuring the props object
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;
}
// Usage:
<Greeting name="Alice" />What will be rendered on the screen?
Solution
Step 1: Identify how props are passed and used
TheGreetingcomponent receives a prop callednamewith value "Alice".Step 2: Understand JSX rendering with props
The JSX uses{props.name}which inserts the string "Alice" inside the <h2> tag.Final Answer:
<h2>Hello, Alice!</h2> -> Option BQuick Check:
props.name = Alice [OK]
- Expecting literal text {name} instead of value
- Confusing props.name with string 'props.name'
- Thinking this causes syntax error
function Welcome(props) {
return <h1>Welcome, props.name!</h1>;
}Solution
Step 1: Check how props are used inside JSX
The code writesprops.nameas plain text inside JSX, so it will render literally instead of the prop value.Step 2: Correct usage of props in JSX
To show the value ofprops.name, it must be wrapped in curly braces like{props.name}.Final Answer:
props.name should be inside curly braces to render its value -> Option AQuick Check:
Use {props.name} to show prop value [OK]
- Writing props.name as plain text
- Thinking function names must be lowercase
- Confusing JSX self-closing rules
Button component that shows different text based on a label prop. Which code correctly uses props to achieve this?function Button(props) {
return <button>???</button>;
}Choose the correct replacement for
???.Solution
Step 1: Understand how to access props inside the component
The component receives props as an object, so the label prop is accessed asprops.label.Step 2: Use curly braces to embed the prop value in JSX
To show the value inside JSX, wrapprops.labelin curly braces:{props.label}.Final Answer:
{props.label} -> Option AQuick Check:
Use {props.propName} to show prop value [OK]
- Using prop name without braces
- Using braces without props object
- Using quotes around prop name
Ready to prove it?
Pass the test and this pattern moves unknown
