🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to React Notes
Topic #46

What is Hooks?


Hooks allow functions to have access to state and other React features without using classes.

They provide a more direct API to React concepts like props, state, context, refs, and lifecycle.


What is a Hook?

Hooks are functions that let you "hook into" React state and lifecycle features from functional components.

Example:

import { useState } from 'react';
import { createRoot } from 'react-dom/client';

function FavoriteColor() {
  const [color, setColor] = useState("red");

  return (
    <>
      <h1>My favorite color is {color}!</h1>
      <button
        type="button"
        onClick={() => setColor("blue")}
      >Blue</button>
      <button
        type="button"
        onClick={() => setColor("red")}
      >Red</button>
      <button
        type="button"
        onClick={() => setColor("pink")}
      >Pink</button>
      <button
        type="button"
        onClick={() => setColor("green")}
      >Green</button>
    </>
  );
}

createRoot(document.getElementById('root')).render(
  <FavoriteColor />
);

You must import Hooks from react.

Here we are using the useState Hook to keep track of the application state.

State generally refers to application data or properties that need to be tracked.


Hook Rules

There are 3 rules for hooks:

  • Hooks can only be called inside React function components.
  • Hooks can only be called at the top level of a component.
  • Hooks cannot be conditional

Note: Hooks will not work in React class components.


Custom Hooks

If you have stateful logic that needs to be reused in several components, you can build your own custom Hooks.

We'll go into more detail in the Custom Hooks section.

Want to go beyond the notes?

Join CodingNow 2.0's React course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

What is Hooks? – FAQs

Quick answers about learning What is Hooks? in React.

This free note from CodingNow 2.0 explains What is Hooks? in React — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every React topic on CodingNow 2.0, including What is Hooks?, is 100% free with no signup required.
With focused practice, most students grasp What is Hooks? in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now