index
Learn how to rewrite older React classes to use the newer hooks
Hooks like useState
, useEffect
(and more) were added to React a couple of years ago. Before that stateful components had to be created using JavaScript classes. It's important to be able to read class-based code since you might encounter it out in the world.
Classes
Classes were added to JS with ES6. They're a special syntax for creating reusable objects with methods and properties.
They can also "extend" other classes to inherit their properties.
Don't worry too much about classes—they're rarely used in React anymore, and even when they were hardly any of their features were used.
Syntax
React class components are created by extending the React.Component
base class:
The render()
method is the equivalent of a function component body. You return React elements from here to render them to the DOM.
Updating state
We can set a class property named state
to tell React to keep track of some values. This property is always an object.
We can access the state object via this.state
.
If we want to update state we call this.setState()
and pass in a new object. React will merge this object with the existing state:
We can also store methods as properties on the class so they're reusable:
this.setState()
can take a function instead of an object if you need to access the previous state value (the same as with React.useState()
).
Effects
Classes don't have a built-in way to deal with side-effects. Instead you have to hook into their "lifecycle" using specially named methods. These function are called at various points by React as it creates your component, puts it into the DOM, updates it or removes it.
For example to run some code when React is ready to render your component to the page we use componentDidMount
:
To run some code when your component updates (i.e. is passed new props or setState
is called) you can use componentDidUpdate()
. To clean up after your component (i.e. cancelling timers or removing global event listeners) you can use componentDidUnmount()
. There are quite a lot of these and you probably won't need them all.
Exercise
Download the starter files and
cd
inRun
npm install
npm test
to start the test watcherRewrite
src/Counter.js
to use hooks instead of classesRewrite
src/Keyboard.js
to use hooks instead of classesRewrite
src/Pokemon.js
to use hooks instead of classesKeep all the tests passing!
Last updated