Introduction
Props let you send information to components so they can show different things. It is like giving a friend a note with instructions.
Props let you send information to components so they can show different things. It is like giving a friend a note with instructions.
function ComponentName(props) { return <div>{props.propertyName}</div>; } // Or using destructuring function ComponentName({ propertyName }) { return <div>{propertyName}</div>; }
Props are passed like attributes in HTML: <ComponentName propertyName="value" />.
You can use curly braces {} inside JSX to show JavaScript values.
function Welcome(props) { return <h1>Hello, {props.name}!</h1>; } // Usage: <Welcome name="Alice" />
label prop.function Button({ label }) { return <button>{label}</button>; } // Usage: <Button label="Click me" />
function Message({ text, color }) { return <p style={{ color: color }}>{text}</p>; } // Usage: <Message text="Hi there!" color="blue" />
This app shows two greetings with different names using the same Greeting component and props.
import React from 'react'; function Greeting({ name }) { return <h2>Hello, {name}!</h2>; } export default function App() { return ( <main> <Greeting name="Sam" /> <Greeting name="Lee" /> </main> ); }
Props are read-only inside the component. To change data, use state instead.
Always use meaningful prop names to keep code clear.
Props send data from parent to child components.
Use curly braces {} in JSX to show prop values.
Props help reuse components with different content.
props in React components?title inside JSX?{props.title}.props object is passed to the component function, so access via {props.title}. Using {title} requires destructuring like function Comp({title}).function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;
}
// Usage:
<Greeting name="Alice" />Greeting component receives a prop called name with value "Alice".{props.name} which inserts the string "Alice" inside the <h2> tag.function Welcome(props) {
return <h1>Welcome, props.name!</h1>;
}props.name as plain text inside JSX, so it will render literally instead of the prop value.props.name, it must be wrapped in curly braces like {props.name}.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>;
}???.props.label.props.label in curly braces: {props.label}.Pass the test and this pattern moves unknown