React Class Components
There are two kinds of components in React - function components and class components. Function components and hooks are the modern way of writing React but there are still a lot of legacy codebases using class components so it's still a must-know for any React developer.
Basic component
Class components must inherit from
Component
and have a render method that returnsjsx
We can access props through
this.props
State & Event Handlers
Initialise the state object either using a
constructor
or the simpler field declaration syntaxAccess state through
this.state
Update state through
this.setState
(same immutable update rules apply)Define event handlers as a method and use an arrow function to bind
this
to the instance
Last updated
Was this helpful?