A Really Simple Intro to useState in React
Tags: react, state, hooks, webdev
Published: 2021-04-04 16:23:19

When it comes to React hooks, useState is one of the most useful. It can be used to capture information that the user inputs and use it elsewhere in your app. 

For example, you could use it to capture the option a user chooses in a dropdown menu or in a series of radio buttons or whether a checkbox is checked or not.

In this post, I'll take you through getting a user's text from a text input field with useState and displaying below.

![Final result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j77fc9rozj6b95sa9imw.jpg)

First, create a new React app and fill it a labelled text input field and some empty paragraph tags, like so -

![create App component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9denacqi69sxdn9e08ui.png)

If you like, you can use the code from my GitHub repo <a href="https://github.com/LauraToddCode/using-usestate/blob/starting-code/src/App.js">here</a>.

We're going to set the app up so that the user's input will appear in the paragraph tags and will update with every keystroke. We'll therefore use the onChange event listener.

So, our first step is to add "onChange=" to our input tag. We'll need to add an event handler function to handle that change (which we'll set up in a later step). For now, just add the name of the handler function you intend to make to the input tag, we'll call ours "handleChange".

**Note:** The naming convention for event handler functions is to use "handle" + "the event" - in this case "handleChange".

![add onChange to input tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn2w4deetsgd2pfzvi2b.png)

Since this post is all about useState, we should probably import it. We do this by adding ", { useState }" after React in the existing import statement for React.

![import useState](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccqjwxviep22kmoc3cml.png)

Next, we need to add useState to our code like so -

![add useState](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6k701b41sheovzjkbo5.png)

Let's break that down a little bit. Starting with the right-hand side, useState() can take an argument which will be used as an initial state. So if we wanted to have some text in our paragraph tags to begin with, we could add it to the useState brackets.

**For example**

![add initial state](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xkhdjxnzderta1tc1xb.png)

Would give us this, until we begin typing into the input box -

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufcwd2zm2shrol1309qm.jpg)

On this occasion, we don't want to initialise state so we'll just leave it as an empty string within the brackets - useState("").

Now, let's look at the left-hand side of the statement. useState() gives us an array with two values and uses array destructuring. If you're not familiar with array destructuring, you can take a look at an explanation in the MDN Web Docs <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">here</a>.

We've called the values in the array "name" and "setName" but you can call them whatever you like. There is a naming convention of using the same description for both values but with "set" at the beginning of the second value.

The first item in the array ("name") is going to be what we use to display or otherwise use the value given to use by the user.

The second item in the array ("setName") is going to be what we use to tell the app where we're getting the information from. We do this within the handler function.

Let's create that handler function now -

![add handler function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpxsnuohk5anl9yat39z.png)

You'll recall that we added an onChange event listener to the input field earlier and gave it a value of "handleChange". Therefore, the input field will call the above function with every keystroke.

Within the function we have given "setName" (from the useState() function) a value. By using "event.target.value", we assign the user's input text to "name".

Finally, we can add the "name" value to the paragraph tags and it will update as the user types in the input field.

![add name value to paragraph tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6indncuiwacqwkbyxuep.png)

You can check your code [here](https://github.com/LauraToddCode/using-usestate/blob/main/src/App.js).

Try and think of other ways that you can use useState() and practice them. The more you do it, the more it will become second nature. You'd be amazed how often this hook can be used in your apps.