React / 40
Class components, the old way
Before hooks, stateful components were JavaScript classes. Learn to read one, why super(props) comes first, and why classes still matter.
The smallest class component
A class component is a JavaScript class that extends React's Component and has a render() method that returns JSX.
import { Component } from "react";
class Greeting extends Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
export default Greeting;You use it exactly like a function component: <Greeting name="Asha" />. The difference is inside: props live on this.props, not in a function parameter. React creates one instance (an object made from the class) per place the component appears, and calls its render() whenever it needs the UI.
class
function
declare
class
class Card extends Componentfunction
function Card(props)read props
class
this.props.namefunction
props.namestate
class
this.state = { open: false }function
useState(false)update
class
this.setState({ open: true })function
setOpen(true)ui
class
render() { return <div/> }function
return <div/>The constructor and super(props)
When a class needs state, the traditional place to set it up is the constructor, the function that runs once when the instance is created.
import { Component } from "react";
class Counter extends Component {
constructor(props) {
super(props); // always first
this.state = { count: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return <button onClick={this.handleClick}>Clicked {this.state.count} times</button>;
}
}Three lines do three jobs:
| Line | Why it's there |
|---|---|
super(props) | Runs the parent class's constructor (React.Component). JavaScript forbids using this in a child class until super() has run: you'd get "Must call super constructor in derived class before accessing 'this'". Passing props lets Component set this.props straight away. Call super() without it and this.props is undefined inside the constructor (React fills it in afterwards, so render still works). |
this.state = { … } | The starting state. This is the only place you assign this.state directly. Everywhere else, use this.setState. |
.bind(this) | Fixes this for the event handler (see below). |
Keep the constructor for those jobs only. No fetching, no timers: React may create an instance and throw it away (in development, Strict Mode calls the constructor twice on purpose to catch side effects).
Why this needs binding
onClick={this.handleClick} hands React the function alone, detached from its object. When React later calls it, this is undefined (class bodies always run in strict mode), so this.setState crashes with "Cannot read properties of undefined".
Modern JavaScript fixes it more neatly with class fields: write state and handlers directly in the class body. An arrow function keeps the this of the place it was written, which is the instance.
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({ count: this.state.count + 1 });
}
// render() as before
}class Counter extends Component {
state = { count: 0 };
handleClick = () => {
this.setState({ count: this.state.count + 1 });
};
// render() as before
}No constructor at all, so no super to forget. You'll see both styles in real code. They behave the same.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
