Beginner's guide to useState hook in React

I am a developer from Delhi, India
Search for a command to run...

I am a developer from Delhi, India
No comments yet. Be the first to comment.
In this series, you'll learn about the basic and essential react hooks which will help you get started with functional components in ReactJS.
A few days ago, I wrote an article on how to use the useState hook. Another important hook provided by React is the useEffect hook. Now, if you've used React Class based Components, then useEffect will help you replace functions like componentDidMoun...
DISCLAIMER: This might not be the most well-written article since I haven't written in a while and I just wanted to get back to writing with a short article about my experience of the day, so sorry about that I previously used this repository for my...

The release of NextJS 13 brought about a plethora of new and impressive features, with one standout being the updated data fetching and management process. The fetch API replaced the more complicated functions, including getServerSideProps, getStatic...

Looking for a safe and reliable way to authenticate users? Consider implementing magic links. They offer a secure alternative to traditional passwords and can help mitigate the risk of password leaks and forgotten passwords. Magic Link authentication...

Neovim is a powerful text editor that can be customized with plugins to enhance its functionality. In this article, we will explore the top 10 essential Neovim plugins. These plugins can help improve your Neovim experience by adding features such as ...

A type-safe and zero-runtime version of Tailwind CSS

A key component of a web application is the state. Taken directly from the beta(new) version of the ReactJS Docs:
Think of state as the minimal set of changing data that your app needs to remember. For example, if you’re building a shopping list, you can store the items as an array in state.
Now, how do we manage state inside a React Component? So, in this tutorial we'll be talking just about that!
First, let's start with what is a React Hook. The beta docs of ReactJS says the following:
Functions starting with use are called Hooks. useState is a built-in Hook provided by React. You can find other built-in Hooks in the React API reference. You can also write your own Hooks by combining the existing ones. Hooks are more restrictive than regular functions. You can only call Hooks at the top level of your components (or other Hooks). If you want to useState in a condition or a loop, extract a new component and put it there.
So basically, if you know a little history about React, you would know that React used to be mainly comprised of something called Class-Based Components. But now, the community is starting to move to Function-Based Components. Now, React Hooks allow the functional components to have access to state and other React Features.
Create a project using the following command
npx create-react-app react-hooks-tutorial
Then edit your src/App.js to look like this
function App() {
return (
<div>
<h1>Hello, World</h1>
<p>1</p> // this will update based on the state after this tutorial
<button>Increase counter</button>
</div>
);
}
export default App;
useState hookNow, the way the useState hook is used in React is very interesting. In a conventional, class based component you'd do something like this
state = {
color: "red"
}
and then access it via this.state. But using the useState hook you'd do something like this
+ import { useState } from "react"
function App() {
+ const [count, setCount] = useState(1);
return (
Now the useState hook returns two things:
setCount is a function which you can use to change the value of the count state.First, update the hardcoded value of 1 with the count variable like so
<h1>Hello, World</h1>
- <p>1</p>
+ <p>{count}</p>
<button>Increase counter</button>
Now to make this work, let's make the click of the button increase the count variable by 1.
<p>{count}</p>
- <button>Increase counter</button>
+ <button onClick={() => setCount(count + 1)}>Increase counter</button>
Now, let's try something. Change the button line to the following
- <button onClick={() => setCount(count + 1)}>Increase counter</button>
+ <button onClick={incrementCount}>Increase counter</button>
const [count, setCount] = useState(1);
+ function incrementCount() {
+ setCount(count + 1);
+ setCount(count + 1);
+ setCount(count + 1);
+ }
Now this will give you an issue. Straight from the docs again (little modified according to our app):
This is because calling the set function does not update the
countstate variable in the already running code. So eachsetCount(count + 1)call becomessetCount(2).
To fix this issue, you can reference the previous state and then update the current state using that. So change your code like this
- function incrementCount() {
- setCount(count + 1);
- setCount(count + 1);
- setCount(count + 1);
- }
+ function incrementCount() {
+ setCount((prevCount) => prevCount + 1);
+ setCount((prevCount) => prevCount + 1);
+ setCount((prevCount) => prevCount + 1);
+ }
So, you get the prevCount argument and increment the current state according to that and this should work!
Note: This is obviously not a real life scenario. To increment by 3, you'd probably do something like
setCount(count+3)but it's good to know when you can use the previous state variable as a reference.
That is it for this tutorial! You final code should look like this:
import { useState } from "react";
function App() {
const [count, setCount] = useState(1);
function incrementCount() {
setCount((prevCount) => prevCount + 1);
setCount((prevCount) => prevCount + 1);
setCount((prevCount) => prevCount + 1);
}
return (
<div>
<h1>Hello, World</h1>
<p>{count}</p>
<button onClick={incrementCount}>Increase counter</button>
</div>
);
}
export default App;