Props Introduction
Props carry data into a component. A prop is a named value passed to a component.
Pass a prop
The name prop carries the text Ada.
<Greeting name="Ada" />Read a prop
props.name holds the passed value.
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>
}Hold the value
A const can hold the prop value for reuse.
const name = props.nameExample
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>
}
export default function App() {
return <Greeting name="Ada" />
}
Key points
- A prop looks like an HTML attribute in JSX.
- The component reads the value from the props object.
- The value can be text or a number.
- A different prop value renders different output.
In this example, Greeting takes props as its first parameter. A parameter is a value a function receives. Greeting reads props.name inside the heading. App passes Ada as the name prop. The page shows Hello, Ada!
Note
Props flow in one direction into the component.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What is a prop?