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

Conteúdo do Curso

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

bookIntroduction to Hooks

In React, a component's state is some data that is tracked by React. Whenever the state changes, React automatically updates the associated component to reflect the new state.

Hooks are functions that allow us to connect to a component's state or lifecycle. With hooks, we can use states and other class component features in functional components, making the code cleaner and simpler. Therefore, we will focus on using functional components with Hooks for simplicity.

We will try to understand hooks by looking at the most basic example. There was a simple button example in the event handling chapter:

We will change it so that it counts the number of clicks.

For that, we create a new state variable called clicks and update the handleClick method to display the relevant text:

In the above code, we declared two constant terms, clicks and updateClicks, using the useState function clicks is the term that holds a value, and updateClicks is the function that is used for updating the value of clicks since it cannot be updated directly – it's a constant.

The neat thing about this is that whenever the value of clicks is changed, React re-renders the component so that we can see the changes.

Note that in the above code, we also imported ‘useState’ from the React module.

If you look at the above component, its output should be something like this:

If you click the button a few times, it should show the number of times it has been clicked. In the next chapter, we will explore how to use hooks in detail.

Which of the following is true about hooks?

Which of the following is true about hooks?

Selecione algumas respostas corretas

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 6. Capítulo 1
some-alt