Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Classes can be a large barrier to learn React, says React team. They donât minify very well and they make hot reloading unreliable. With React Hooks, you donât need to convert a function component into class component. You can use state and lifecycle methods in the function component.
Okay, but what are hooks really?
They are functions that let you âhookâ into React internal state and lifecycle features from function components
Great! How do I do that?
First, Update your React and React DOM dependency in your project. Hooks are freshly released on React 16.8.0Â today!
npm install --save react@^16.8.0 react-dom@^16.8.0
Now letâs take a look on the useState hook. Here is the demo:
useState hook
We need to have a simple class component with state for comparison. The easiest example I can imagine is of an input form:
class NameForm extends React.Component { constructor(props) { super(props); this.state = { value: '' }; }
handleChange = event => { this.setState({ value: event.target.value }); }
render() { return ( <form> <label> Name: <input type="text" value={this.state.value} onChange={this.handleChange} /> </label> <input type="submit" value="Submit" /> </form> ); }}
Now letâs rewrite it using useState hook. We'll import it from react package so we don't have to write React.useState all the time.
import React, { useState } from 'react';
Now change our NameForm into a function component
function NameForm(props) {
}
useState hook takes one argument, which is the initial state, and it returns two values: the current state and a function that can be used to update the state. You can replace the state initialization in the constructor:
this.state = { value: '' };
into this:
function NameForm(props) { const [value, setValue] = useState(''); }
Notice the use of square brackets [] when state variable is declared. This is the ES6 âarray destructuringâ syntax, and it means weâre assigning the first value returned by useState to value and the second value to setValue.
So this means we have a state named value and we can update it by calling on setValue function. Letâs use it on our render method:
function NameForm(props) {const [value, setValue] = useState('');
return ( <form> <label> Name: <input type="text" value={value} onChange={event => (setValue(event.target.value))} /> </label> <input type="submit" value="Submit" /> </form> )}
Onchange props no longer call a handleChange method, instead we have an arrow function that will call setValue function, which update our state. Oh, and unlike this.setState in the class component, updating a state variable using hooks always replace it instead of merging it.
Having multiple states?
Then call on useState as many times as you need.
function SimpleForm(props) {const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [age, setAge] = useState('');
return ( <form> <label> First Name: <input type="text" value={firstName} onChange={event => (setFirstName(event.target.value))}/> </label> <label> Last Name: <input type="text" value={lastName} onChange={event => (setLastName(event.target.value))} /> </label> <label> Age: <input type="number" value={age} onChange={event => (setAge(event.target.value))} /> </label> <input type="submit" value="Submit" /> </form> )}
And thatâs it with useState hook, really!
Setting initial state from props
Oh, itâs pretty easy!
function SimpleForm(props) { const [firstName, setFirstName] = useState(props.firstName); const [lastName, setLastName] = useState(props.lastName); const [age, setAge] = useState(props.age);
//...}
ReactDOM.render( <SimpleForm firstName="JOHN" lastName="Edward" age={30} />, document.getElementById("root"));
Can I use object in state?
Sure! Just as class state can accept object or array, useState can have them as well. But to make it work, letâs add name props to your input elements. We also use spread properties to update our state.
function SimpleForm(props) { //create object stateconst [form, setForm] = useState({ FirstName: '', LastName:'', age:'' });
const handleChange = event => { // use spread operator setForm({ ...form, [event.target.name]: event.target.value }); }
return ( <form> <label> First Name: <input type="text" name="firstName" value={form.firstName} onChange={handleChange} /> </label> <label> Last Name: <input type="text" name="lastName" value={form.lastName} onChange={handleChange} /> </label> <label> Age: <input type="number" name="age" value={form.age} onChange={handleChange} /> </label> <input type="submit" value="Submit" /> </form> )}
What about array?
This oneâs a bit unusual, but yes, just like in class state. Weâll use concat on array update.
function SampleArrayState(props) { //create array stateconst [nameArray, setNameArray] = useState(["Carl"]);
const updateArray = () => { setNameArray(nameArray.concat("John")); };
return ( <React.Fragment> <button onClick={updateArray}>Click me!</button> <div>{nameArray.toString()}</div> </React.Fragment> );}
And Boolean?
Got you covered here:
function SampleBooleanState(props) {const [show, setShow] = useState(true); const visibility = show ? 'visible' : 'hidden';
return ( <React.Fragment><h1 style={{ visibility }}>useState Hook is awesome!</h1> <button onClick={() => { setShow(!show) }} >{`${show ? 'Hide' : 'Show'} the Header!`}</button> </React.Fragment> );}
The rules of hooks
The important thing to remember is that hooks donât work in a class component. They are made for function components.
- Donât call hooks from inside nested function, loops or conditionals.
- Donât call hooks from regular JavaScript function.
- This
Conclusion
The useState hook enables function components to access Reactâs internal state and update it. The state can be any data type: string, number, boolean, array, or object. useState accept one argument: the initial data, and it returns an array of two values: the current state value and the function/ method that can be used to update the state.
There are more than just useState hook, but letâs cover each hook in their own single post.
Hereâs a list of all built-in hooks (yes, you can write a custom hook!) available as of this writing. Iâll be covering them in the next posts.
Until next time!
Also, Iâm writing a book on learning React properly without the stress. You might wanna check it out here.
Originally published at sebhastian.com.
React Hooks are live. Here is your introduction was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.