Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Using the State Hook | Hooks
Introduction to React
course content

Course Content

Introduction to React

Introduction to React

1. Getting Started: ES6
2. Getting Started: JSX
3. React in Practice
4. React Components
5. Controlling Data & Content
6. Hooks
7. React Router

bookUsing the State Hook

In this chapter, we will look at the syntax for using the State hook. The state hook allows us to use states in React. The syntax for creating a state variable is the following:

The useState function creates a new state with an initialValue that can range from an integer to a complex object.

It returns two things – the value of the created state and the reference to a function that can update that value.

We typically use the array deconstructing method to create two constants for storing these values.

In the above code, the constant variable will hold the value of the state, while the updateFunction can be used for updating the value of the variable. For example, we had the following code in the previous chapter:

In the above code, you can see a state variable called clicks is being initiated with a value of 1 - as the value 1 is being passed into the useState function.

In the handleClick function, the updateClicks function is used to increment the value of clicks.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 2
some-alt