Introduction
Props let you pass information from one component to another in React. They help components share data and work together.
Props let you pass information from one component to another in React. They help components share data and work together.
function ComponentName(props) { return <div>{props.someValue}</div>; } // Usage <ComponentName someValue="Hello" />
function Welcome(props) { return <h1>Hello, {props.name}!</h1>; } <Welcome name="Alice" />
function Button(props) { return <button style={{ backgroundColor: props.color }}>{props.label}</button>; } <Button label="Click me" color="blue" />
function Message({ text }) { return <p>{text}</p>; } <Message text="Welcome to React!" />
This app shows two greetings with different names passed as props.
import React from 'react'; function Greeting(props) { return <h2>Hello, {props.user}!</h2>; } export default function App() { return ( <div> <Greeting user="Sam" /> <Greeting user="Lee" /> </div> ); }
Props cannot be changed inside the component that receives them.
Use props to make components reusable and flexible.
Props pass data from parent to child components.
They are like function arguments for components.
Props help components show different content without rewriting code.
props in React?title with value "Hello" to a component Greeting?title="Hello". misses the key, C uses unquoted value, D misses quotes.function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage:
<Welcome name="Alice" />props.name inside JSX to display the passed name.name="Alice", so props.name equals "Alice".function Message(props) {
return <p>{props.text}</p>;
}
// Usage:
<Message text=Hello />text=Hello is missing quotes around the string value.text="Hello" or wrapped in curly braces if a variable.Button component that shows different labels based on props. Which approach correctly uses props to achieve this?function Button(props) {
return <button>{props.label}</button>;
}
// Usage:
<Button label="Save" />
<Button label="Cancel" />Button component uses props.label inside the button, and different labels are passed in usage.Pass the test and this pattern moves unknown