JavaScript

React Hooks

Make your components alive! Master useState and useEffect to handle state and side effects.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Make your components alive! Master useState and useEffect to handle state and side effects. This hands-on tutorial focuses on practical implementation of react hooks concepts.

React Hooks

Before 2018, React components were hard to reuse. Then came Hooks. Hooks allow you to "hook into" React features like State and Lifecycle methods from simple functions.

1. useState: Managing Data πŸ’Ύ

State is the "memory" of a component. When state changes, the component re-renders (updates).

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

How it works:

  • count: The current value.
  • setCount: The function to update the value.
  • useState(0): The initial value is 0.

2. useEffect: Side Effects ⚑

Components should be "pure" (input -> output). But sometimes we need to reach outside:

  • Fetching data from an API.
  • Setting a timer.
  • Changing the document title.

These are called Side Effects.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

The Dependency Array []:

  • []: Run once (like componentDidMount).
  • [count]: Run whenever count changes.
  • (No array): Run after every render (Be careful!).

3. useRef: Accessing the DOM πŸ“

Sometimes you need to grab an HTML element directly (like focusing an input).

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

Note: Changing a ref does not trigger a re-render.

4. Rules of Hooks πŸ“œ

  1. Only Call Hooks at the Top Level: Don't call them inside loops, conditions, or nested functions.
  2. Only Call Hooks from React Functions: Don't call them from regular JS functions.

AI Mentor

Confused about "React Hooks, useState, useEffect, useRef, and custom hooks"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 4

What does useState return?

Just the value
Just the setter function
An array: [value, setter]
An object

Key Takeaways

βœ… useState handles dynamic data.
βœ… useEffect handles API calls and timers.
βœ… useRef touches the DOM.
βœ… Rules of Hooks keep things stable.

What's Next?

Now that we have the tools, let's learn how to organize them with Component Patterns.

Keep coding! πŸš€