this.props: Accessing Props in Classes
What you'll learn: How to access props inside class components using this.props instead of function parameters.
In function components, props arrive as a function parameter. In class components, React makes props available as a property on the instance — specifically this.props.
The Core Difference
When you write a class component, you don't receive props as an argument to render(). Instead, React attaches props to the class instance itself. You access them with this.props:
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
Compare this to the function version:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
Why this?
The this keyword refers to the instance of your component class. When React creates your component, it passes props to the constructor and stores them on the instance. Throughout the component's lifecycle, this.props always points to the current props object.
Reading Multiple Props
Access any prop using dot notation:
class UserCard extends React.Component {
render() {
return (
<div>
<h2>{this.props.username}</h2>
<p>Email: {this.props.email}</p>
<p>Age: {this.props.age}</p>
</div>
);
}
}
Remember: Props Are Still Read-Only
Just like in function components, you cannot modify this.props. React enforces the same immutability rule:
// ❌ This will error
this.props.name = "New Name";
Key Takeaway: In class components, props are accessed via this.props instead of function parameters, requiring you to reference the component instance using this throughout your render method and other class methods.