August 30, 2021

πŸͺ React Hooks – A Beginner-Friendly Guide

React Hooks changed the way we write React apps. They make function components powerful and simple to use. If you’re still using class components for everything, it might be time to explore Hooks!


πŸ€” What Are Hooks?

Hooks are functions that let you β€œhook into” React features like state, lifecycle, and context in function components.

Before Hooks, if you wanted to use state or componentDidMount, you had to write a class component. Now, with Hooks, function components can do everything.


πŸ”₯ Why Use Hooks?

Here are some reasons developers love Hooks:

πŸ‘ BenefitπŸ“˜ Description
Less CodeNo need to write classes, constructors, or bind methods
Easier to ReadFunctions are smaller and cleaner
Reusable LogicYou can make custom hooks to share logic between components
RecommendedReact team recommends function components + hooks

πŸ› οΈ Most Common Hooks

Here are the most commonly used built-in hooks:

1. useState – Add State to a Function Component

import React, { useState } from 'react';

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

  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}

βœ… useState(0) means count starts at 0
βœ… setCount() updates the value
βœ… React re-renders the component when the state changes


2. useEffect – Side Effects (like fetch, timer)

import React, { useEffect } from 'react';

function MyComponent() {
  useEffect(() => {
    console.log('Component mounted');

    return () => {
      console.log('Component will unmount');
    };
  }, []); // empty array = run only once

  return <p>Hello World!</p>;
}

βœ… useEffect() runs after the component is shown
βœ… It replaces componentDidMount, componentDidUpdate, and componentWillUnmount


3. useRef – Keep a Value Between Renders (or Access DOM)

import React, { useRef } from 'react';

function FocusInput() {
  const inputRef = useRef();

  function handleClick() {
    inputRef.current.focus(); // focus the input box
  }

  return (
    <>
      <input ref={inputRef} />
      <button onClick={handleClick}>Focus</button>
    </>
  );
}

βœ… useRef gives you access to DOM elements
βœ… It can also store values without causing re-render


4. useContext – Use Global Values

const ThemeContext = React.createContext();

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Header />
    </ThemeContext.Provider>
  );
}

function Header() {
  const theme = React.useContext(ThemeContext);
  return <h1>The theme is {theme}</h1>;
}

βœ… useContext() helps you use shared data (like theme or user)
βœ… No need to pass props again and again


πŸ§ͺ Bonus: Custom Hooks

You can create your own hook like this:

function useDocumentTitle(title) {
  useEffect(() => {
    document.title = title;
  }, [title]);
}

And use it in any component:

function MyPage() {
  useDocumentTitle("My Page");
  return <h1>Welcome</h1>;
}

🏁 Summary

HookUse For
useStateAdd and update data
useEffectRun code after render (side effects)
useRefKeep values or get DOM elements
useContextUse shared/global data
Custom HookReuse logic between components

πŸ“Œ Final Thoughts

React Hooks made React development easier and cleaner. You no longer need classes to manage complex features. If you’re still new to React or just moving from class components, Hooks are the future β€” and they’re beginner-friendly too.