What are hooks in React?

What are hooks in React?

React is a popular JavaScript library for building user interfaces. It allows developers to create reusable components that can be easily shared and integrated into other applications. One of the key features of React is its ability to manage state and allow components to communicate with each other.

Hooks are a new feature in React that was introduced in version 16.8. They provide a way to use state and other React features without writing a class component. This means that you can use state and other features in functional components, which are simpler and easier to write and understand.

There are several built-in hooks in React, including:

  • useState: This hook allows you to add state to a functional component. It returns an array with two elements: the current state value and a function to update it.

  • useEffect: This hook allows you to perform side effects in functional components. It is similar to componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

  • useContext: This hook allows you to consume context in a functional component. It makes it easier to share values between components without having to pass props through multiple levels of the component tree.

  • useReducer: This hook is similar to useState, but it is designed for managing state that is more complex or involves multiple sub-values. It accepts a reducer function and returns the current state value and a dispatch function to update it.

Here is an example of a functional component using the useState hook to add a counter with a button that increments the count:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(prevState => prevState + 1)}>
        Click me


Hooks are a powerful and flexible way to add state and other features to functional components in React. They make it easier to write and understand your code, and they allow you to reuse stateful logic across your application. If you are new to React, or if you are looking for a simpler way to add state to your components, hooks are definitely worth checking out.

Thank you for reading! See you again soon.

Please feel free to reach out to me on Twitter.

You can visit my Portfolio to check out the projects I have worked on.

Did you find this article valuable?

Support Bhagya Mudgal by becoming a sponsor. Any amount is appreciated!