DEV Community

Cover image for What is hooks in React JS?
Sanajit Jana
Sanajit Jana

Posted on • Edited on

What is hooks in React JS?

In this article, you will learn what are hooks in React JS? and when to use react hooks? React JS is developed by Facebook in the year 2013. There are many students and the new developers who have confusion between react and hooks in react. Well, it is not different, react is a programming language and hooks is a function that is used in react programming language.

Connect with me
Portfolio LinkedIn GitHub

When were hooks introduce?

React hooks are introduced in version 16.8 which is the recent Update in the react programming language. The concept of hooks has made the programming so easier for the react developers that everyone is now adapting the Hooks concept in their programming.

React hooks are always utilized in a useState and other react features without writing a class. React hooks are the functions that hook into the react state life-cycle features from the function components.

Like other features in react hooks does not work inside the classes it needs to be separated from the classes and used in the code. The hooks function must be utilized at the top of the react functions as it makes a clear vision of the program we want to execute in the project. You can use hooks without classes in the react programming and you can create your own hook to reuse the state full behaviour of different components in reactive programming.

If we want to try any code related to hooks, we need to write it in the functional component itself. If we write out of the functional component it will show us an error (invalid hooks call on the web page) so to avoid that error make sure that you write the hooks in the functional component.

Alt Text

What are the requirements to use react hooks?

As we have mentioned that the react hooks are introduced in the version of 16.8. To make sure that the react hooks works properly. The developer should make sure that he or she is using the NODE version of 6 or above and the NPM version 5.2 or above. If the versions are below this criterion the react hooks will not work exactly the way you wish to see it. It is very important to know when to use react hooks.

Let’s write a small program of Hooks function using a useState. We will be making a program of increasing numbers on the button click.

When you add useState you will see that an extension will be added at the top of react. Let’s see react hooks example:

import React, { useState } from react;
Enter fullscreen mode Exit fullscreen mode

Syntax:-

const [count, setCount] = useState(0);
const ButtonClick = () => {
setCount(count + 1);
};
Enter fullscreen mode Exit fullscreen mode

Now in the HTML code, you have to write:

<h1>{count}</h1>
<button onClick= {ButtonClick}> Click Me </button>
Enter fullscreen mode Exit fullscreen mode

So this is all about what are hooks in React JS? and react hooks example. If you have any questions you can ask in the comment Info At One always try our best to help you with it…

Connect with me
Portfolio LinkedIn GitHub

Top comments (0)