Class Fields Syntax: Public Properties
You'll learn how class fields eliminated the boilerplate of constructors and binding before hooks made class components mostly obsolete.
The Repetitive Pain They Solved
Remember from the previous lesson how class components forced you to write a constructor(), call super(props), initialize state with this.state = {}, and manually bind event handlers with this.functionName = this.functionName.bind(this)? That was a lot of ceremony for something so common.
What Class Fields Syntax Did
Class fields syntax (a modern JavaScript feature) let you declare properties directly in the class body, outside any method:
class Counter extends React.Component {
// State initialized without a constructor!
state = { count: 0 };
// Arrow function stored as property — automatically bound!
handleClick = () => {
this.setState({ count: this.state.count + 1 });
}
render() {
return <button onClick={this.handleClick}>{this.state.count}</button>;
}
}
No constructor needed. No bind() calls needed. The arrow function syntax for handleClick uses lexical this, so it's always bound to the component instance.
Why This Mattered (Briefly)
For a few years, this was the cleanest way to write class components. It reduced 8-10 lines of boilerplate to 2-3 lines. Every tutorial recommended it.
Then hooks arrived, and function components became the standard. Now class fields are mostly historical knowledge — you'll see them in older codebases, but you won't write new components this way.
Key Takeaway: Class fields let you skip constructors and manual binding by declaring state and arrow-function methods directly in the class body, but hooks have since made this entire approach unnecessary.