Hooked on React Hooks — useStateSo what’s all the rage with React hooks? I’ve been exploring it for the past few months and lets just say I really believe that this is what React has needed to surmount its position as the top front end framework.
Setup your project as shown in this post.
Then lets npm install --save react@^16.8.0 react-dom@^16.8.0
If your adding this to an existing codebase its super important to make sure that the react and react-dom versions are the same. We had some issues where if was throwing an error from the server side not recognising hooks.
Here we have a component ColourPicker. It toggles between the three colours chosen, with the default on red.
import React, { Component } from 'react';
import './ColourPicker.css';
class ColourPicker extends Component {
constructor() {
super();
this.state = {
colour: 'Red',
};
}
colourChoice = (colour) => {
this.props.handleColour(colour);
this.setState({ colour: colour });
};
render() {
return (
<div className="colour-picker">
<p>
Colour:{' '}
<span className="picker-choice">{this.state.colour}</span>
</p>
<button onClick={() => this.colourChoice('Blue')} />
<button onClick={() => this.colourChoice('Cyan')} />
<button onClick={() => this.colourChoice('Pink')} />
</div>
);
}
}
export default ColourPicker;
When a button is clicked, it triggers the propped function handleColour from the parent component which will do something there. Now lets check out the React hooks refactor;
import React, { useState } from "react";
import "./ColourPicker.css";
const ColourPicker = ({ handleColour }) => {
const [colour, setColour] = useState("Red");
const colourChoice = (colour) => {
setColour(colour);
handleColour(colour);
};
return (
<div className="colour-picker">
<p>
{`Colour: `}
<span className="picker-choice">{colour}</span>
</p>
<button onClick={() => colourChoice("Blue")} />
<button onClick={() => colourChoice("Cyan")} />
<button onClick={() => colourChoice("Pink")} />
</div>
);
};
export default ColourPicker;
First thing we should notice is that we import useState at the top. This is how you import each hook that you want to use.
Next you should notice this line;
const [colour, setColour] = useState("Red");
This array destructuring is going to be the format we use the old setState with. There are three parameters we want to add in for every state we want to use, the name of the state, the function name of changing the state, and the state’s default value.
const [stateName, setStateNameNewState] = useState(defaultValue);
With this in mind, check out the function colourChoice that is called on click;
const colourChoice = (colour) => {
setColour(colour);
handleColour(colour);
};
compared to the old
colourChoice = colour => {
this.props.handleColour(colour);
this.setState({ colour: colour });
};
As you can see its much nicer readable syntax.
There is no need to always put in a key: value pair anymore, and notice the lack of this and this.props keywords! So much better!
Useful References
There are some great docs about React and hooks, check out these awesome references if you wanna read more.
Next post we’ll check out how hooks deal with lifecycle hooks.