May 2019
Intermediate to advanced
316 pages
7h 13m
English
React hooks let you use the state in your components without using a class. You can write your components by using ES6 arrow functions. Let's take a look at how the previous counter example (mentioned in Props and the state section ) can be performed using React hooks. The hook function that lets you set a state value is called useState. It takes one argument, which is the initial value of the state. The following example code creates a state variable called count, and the initial value is zero.
The value of the state can now be updated by using the setCount function:
// count state with initial value 0 const [count, setCount] = useState(0);
The counter example now looks like the following when using setState. Here, we don't use ...
Read now
Unlock full access