"Props" is short for properties. They are a fundamental concept in React for passing data from parent components to child components. Think of props as arguments you pass to a function (your component).
Props are read-only from the perspective of the child component. This means a child component should never modify the props it receives directly. This principle helps enforce a unidirectional data flow, which makes your application easier to understand and debug.
How to Use Props:
// Parent Component
function App() {
return <Greeting name="Alice" />;
}
// Child Component (Function Component)
function Greeting(props) { // props is an object: { name: "Alice" }
return <h1>Hello, {props.name}!</h1>;
}
// Child Component (Class Component)
class GreetingClass extends React.Component {
render() { // Access props via this.props
return <h1>Hello, {this.props.name}!</h1>;
}
}You can pass any JavaScript value as a prop, including strings, numbers, booleans, arrays, objects, and even functions (which is how child components can communicate back to parent components).
What are 'props' in React components?
How are props passed to a child component in JSX?
Are props mutable within the child component that receives them?