惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

DEV Community

Why Code Golfing is the Ultimate Test for Multimodal LLMs (And a New Benchmark to Prove It) Decoding Solana Account Data: Three Methods Compared MCP Just Landed on Your Phone: What Google AI Edge Gallery Actually Does I Made My Website "Alive" using Physics (Vanilla JS Experiment Part 2) 🚀 Stop Asking “Which Model?” and Start Fixing Your Team’s AI Supply Chain [Image Test C] How to Prompt AI Tools to Write Accurate SQL Queries (And Why Most Developers Get This Wrong) Stop Asking “Which Model?” and Start Fixing Your Team’s AI Supply Chain [Image Test B] Stop Asking “Which Model?” and Start Fixing Your Team’s AI Supply Chain [Image Test A] PayPal and Stripe Are Not the Answer for Global Digital Sales Signs your WordPress site needs a headless CMS rebuild Sanity CMS vs Contentful for Next.js projects: an honest comparison Sanity vs Strapi vs Payload CMS: an honest comparison for 2026 Sanity CMS website cost in 2026: what founders actually pay INP for React Apps: Profiling and Eliminating Long Tasks Why Core Web Vitals Matter (and How I Improve Them) Why AI Agents Love Boring Code I got tired of manual WordPress maintenance across 8 client sites - so I automated all of it My PR Merged Into a Graveyard: On the Rise of Antigravity and the Fall of Open Source Private AI on a Normal Android Phone: Building Krexel with Gemma 4 E2B The Best Result This Week Was a Failed Prediction — Phase-3a Doesn't Transfer Embedding 685 million texts in 32 minutes I Asked the Top 6 AI Chatbots to Sell Me on Themselves - Then Asked Each One Who Came Second Hello World JahSeeToo The First Malaysia's Hacker i watched google tear down the old internet from a hostel room in kolkata How I audit and prune unused Sanity document types to reclaim Studio performance What is MCP, and why it's the missing layer between AI and your CRM Stop adding print statements to debug your data pipeline — use watcher instead Hire a Sanity developer vs agency: five honest trade-offs Temporal vs Make for API-First Workflows The Antigravity 2.0 Forced Update: How to Fix the Broken Editor Loop 10 Ways To Reduce Your LLM API Costs mcp-probe v1.0.0: A CI readiness gate for MCP servers Building ValoVault: The Per-Agent Skin Loadouts Riot Never Shipped Most CMS Platforms Aren’t Built for Full Lifecycle Ownership 45 MB of Claude Code Sessions You Don't See Building a Resilient Checkout in NestJS: Retry, Idempotency, and a System That Tunes Itself Html learning journey I built an open-source alternative to ViciDial. Here's the stack, and the bugs that ate my nights. Zero-PC Architecture: Deploying Webhooks & AI Triage from a Mobile Footprint Why AI Coding Agents Fail Senior Engineers (And What I Built to Fix It) Stop Pasting URLs into Security Header Sites - Use This CLI 26 of 39 AI Companies Use SPF Softfail — Their Email Can Be Spoofed Mastering useRef in React: The Hook That Gives React Memory Without Re-Rendering One Brain, Many Hands: Building a Parallel Task Orchestrator for AI Agents Understanding useRef in React: Concepts, Use Cases, and Examples An AI That Can't Trade, a Human That Can't Say No SSH died. Spent 3 hours fixing the wrong thing. ## Rise of the Managed Agent: Why Antigravity 2.0 is Google I/O 2026’s Most Critical Developer Release From Concept to Production: A Technical Guide to Deploying Markus Multi-Agent Systems First Principles Why Browsers Outpaced Web Tooling (And How We Catch Up) Building a Safety-First RAG Triage Agent in Python Gemma 4 Isn’t Just Another AI Model — It’s A Shift In How We Build AI The Feature Store: Consistency and Latency Are Both Non-Negotiable What did gemma see? - Thinking in comments... I Built a Desktop Chat App for Running Local LLMs Offline Alert Fatigue Is a Design Choice: Building Views That Actually Help Building A Laravel Google Sheets Package That Imports, Exports, Caches, Formats, And Tests Cleanly DOM Accessibility Tree Extraction: A Reliable Method for LLMs on Dynamic Web Tables Building a Production Grade AWS Infrastructure Project (Part 1) Google just shifted the agent workflow from the cloud to the desktop I built a Claude skill that keeps your AI coding tools from contradicting each other — and I need beta testers Google I/O 2026 - Day 1 - Live from the Front Row The Effect of Frosted Glass (Glassmorphism) in Pure CSS in 2026 Solana's Account Types Are Just Database Rows With Different Flags Gemini vs. ChatGPT for Coding: A Developer's Guide Cryptographic Forensics for AI Coding Agent Sessions Testing NGB Platform Beyond a Small Demo Dataset with k6 and TypeScript Metabase 61: AI fun police, build questions and dashboards with MCP, and much more! How GBase 8a Rough Index Works: Block‑Level Pruning for 10x Faster Queries The Anti-Antigravity Bulkhead vs Rate limiting. The Age of Accountable Agents: Building Trust in Your AI Automation Securing Your AI Agents: Essential Practices for On-Device Automation I benchmarked OpenAI's new GPT-Realtime-Translate against four other live translation systems The Code Nobody Will Delete Building a desktop studio for interactive video stories like Late Shift - Devlog #1 Solving the Local AI Sandbox Issue: How TaigaAI Keeps Your Workstation Safe Why Enterprises Will Struggle With MCP — And What to Do About It Why I Finally Added a Blog to My Converter Tool When Your Coding Agent's String-Matcher Becomes a Billing Decision Building ThreatPulse IDS: An AI-Powered Intrusion Detection System I Built a Register-VM JavaScript Engine in Rust with opencode.ai — Beating QuickJS Per-User OAuth for AI Agents: Why It Matters and What to Look For You Got Your Whole Genome Sequenced. Now What? Zero to Full-Stack in 6 Months: The Izzy Way... PasteCheck v1.3 — what I improved after launching and getting real users DeepSeek V4 on Huawei's Ascend 950: A Real Stress Test for China's AI Chip Ecosystem How Strong Is "Strong"? Password Entropy in Plain English Precision Mechatronics: Mitigating Step-Pulse Resonance and Thermal Dissipation in Micro-Stepping Hardware Controllers A Fact A Day, an autonomous Podcast as my entry 4 Hermes Agent Challenge #100DaysOfSolana Day29: My Experience Generating Token On Solana Devnet Overcoming Challenges and Applying Best Practices in Migrating Large JavaScript Codebases to TypeScript Decostruire lo Streaming di FC2: Come Costruire un Downloader ad Alte Prestazioni con HLS e WebAssembly Top 10 Agentic AI Frameworks Compared: LangGraph vs CrewAI vs AutoGen vs... (Benchmarks Inside) How I Built a Hermes Agent for Lead Generation That Finds and Qualifies Better Prospects The Hybrid Method: when Claude.ai supervises Claude Code LLMs Are Probabilistic. Your Workflow Shouldn't Be. Deploying Tempo Distributed Tracing Backend on Ubuntu 24.04
Build Reusable Logic with Custom Hooks in React
Kathirvel S · 2026-05-18 · via DEV Community

Welcome back to the “Let’s Master React Hooks Together” series.

So far in this series, we’ve explored several important React Hooks and understood how they help manage state, side effects, performance optimization, references, and more inside React applications.

Now we’ve reached one of the most practical and powerful topics in React development — Custom Hooks.

This is the point where React applications start becoming cleaner, smarter, and more reusable.

As applications grow, developers often notice the same logic repeating in multiple components:

  • Fetching API data
  • Managing forms
  • Handling authentication
  • Tracking screen size
  • Working with local storage
  • Managing counters and timers

Instead of rewriting the same logic again and again, React allows us to extract that logic into reusable functions called Custom Hooks.

In this episode, we’ll deeply understand:

  • What Custom Hooks are
  • The official React definition
  • Why they are important
  • When to use them
  • How to create them
  • Real-world examples
  • Best practices
  • Common mistakes to avoid

Let’s get started.


Custom Hooks in React: Reusing Logic the Smart Way

When building React applications, one thing becomes obvious very quickly: the same logic appears again and again.

You fetch data in multiple components.
You track window size in several places.
You handle form inputs repeatedly.
You manage loading states everywhere.

At first, copying and pasting feels fast. But after a while, your code becomes difficult to maintain.

That’s exactly where Custom Hooks become useful.

Custom Hooks are one of the most powerful patterns in React because they allow you to extract reusable logic into a clean and reusable function.

In this article, we’ll understand:

  • What Custom Hooks are
  • The official React definition
  • Why they exist
  • When to use them
  • How to create them
  • Where they are useful in real projects
  • Best practices and common mistakes
  • Real-world examples

What Is a Custom Hook?

According to the official React documentation:

“A custom Hook is a JavaScript function whose name starts with use and that may call other Hooks.”

At first glance, this definition feels simple. But there’s more behind it.

A Custom Hook is basically:

  • A reusable function
  • That contains React logic
  • And can internally use hooks like useState, useEffect, useContext, etc.

Instead of duplicating logic across components, you move that logic into a reusable hook.


Why Custom Hooks Exist

To understand why Custom Hooks matter, let’s first look at a common problem.

Imagine you have multiple components fetching data from an API.

import { useState, useEffect } from "react";

function Users() {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then((res) => res.json())
      .then((data) => setUsers(data));
  }, []);

  return (
    <div>
      {users.map((user) => (
        <p key={user.id}>{user.name}</p>
      ))}
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Now another component needs similar fetching logic.

import { useState, useEffect } from "react";

function Posts() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/posts")
      .then((res) => res.json())
      .then((data) => setPosts(data));
  }, []);

  return (
    <div>
      {posts.map((post) => (
        <p key={post.id}>{post.title}</p>
      ))}
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Notice something?

The fetching logic is almost identical.

This creates problems:

  • Duplicate code
  • Harder maintenance
  • Repeated bug fixes
  • Larger components
  • Poor readability

Custom Hooks solve this by extracting reusable behavior.


Understanding the Real Purpose of Custom Hooks

A lot of developers think Custom Hooks are only for reducing code duplication.

That’s true — but not the full picture.

The real purpose is:

1. Reusability

Write logic once and use it anywhere.

2. Separation of Concerns

Your component focuses on UI while the hook handles logic.

3. Cleaner Components

Components become smaller and easier to read.

4. Better Maintainability

Updating logic in one place updates it everywhere.

5. Easier Testing

You can test business logic separately from UI.


The Rules of Custom Hooks

Custom Hooks follow the same rules as React Hooks.

Rule 1: The function name must start with use

Correct:

useFetchData()

Enter fullscreen mode Exit fullscreen mode

Wrong:

fetchData()

Enter fullscreen mode Exit fullscreen mode

React relies on this naming convention internally.


Rule 2: Hooks must be called at the top level

Do not call hooks inside:

  • loops
  • conditions
  • nested functions

Wrong:

if (show) {
  useEffect(() => {});
}

Enter fullscreen mode Exit fullscreen mode

Correct:

useEffect(() => {
  if (show) {
    // logic here
  }
}, [show]);

Enter fullscreen mode Exit fullscreen mode


How to Create a Custom Hook

The structure is very simple.

function useSomething() {
  // hook logic

  return something;
}

Enter fullscreen mode Exit fullscreen mode

That’s it.

Now let’s build a real example.


Example 1: Creating a useFetch Hook

Step 1: Create the Hook

import { useEffect, useState } from "react";

function useFetch(url) {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    async function fetchData() {
      try {
        setLoading(true);

        const response = await fetch(url);

        if (!response.ok) {
          throw new Error("Failed to fetch data");
        }

        const result = await response.json();

        setData(result);
      } catch (err) {
        setError(err.message);
      } finally {
        setLoading(false);
      }
    }

    fetchData();
  }, [url]);

  return { data, loading, error };
}

export default useFetch;

Enter fullscreen mode Exit fullscreen mode


Step 2: Use the Hook

import useFetch from "./useFetch";

function Users() {
  const { data, loading, error } = useFetch(
    "https://jsonplaceholder.typicode.com/users"
  );

  if (loading) return <p>Loading...</p>;

  if (error) return <p>{error}</p>;

  return (
    <div>
      {data.map((user) => (
        <p key={user.id}>{user.name}</p>
      ))}
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Now the component is much cleaner.

The fetching logic lives in one reusable place.


Example 2: Creating a useCounter Hook

One of the simplest and most common examples of a Custom Hook is a counter.

Instead of managing counter logic separately in every component, we can create a reusable hook.

Step 1: Create the Hook

import { useState } from "react";

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

  const increment = () => {
    setCount((prev) => prev + 1);
  };

  const decrement = () => {
    setCount((prev) => prev - 1);
  };

  const reset = () => {
    setCount(initialValue);
  };

  return {
    count,
    increment,
    decrement,
    reset,
  };
}

export default useCounter;

Enter fullscreen mode Exit fullscreen mode


Step 2: Use the Hook

import useCounter from "./useCounter";

function Counter() {
  const {
    count,
    increment,
    decrement,
    reset,
  } = useCounter(10);

  return (
    <div>
      <h2>{count}</h2>

      <button onClick={increment}>
        Increment
      </button>

      <button onClick={decrement}>
        Decrement
      </button>

      <button onClick={reset}>
        Reset
      </button>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Now the counter logic is reusable across multiple components.

You can use the same hook anywhere in your application without rewriting the logic again.


When Should You Create a Custom Hook?

Not every repeated line of code needs a Custom Hook.

A good Custom Hook usually appears when:

You repeat logic across multiple components

Example:

  • API fetching
  • Authentication
  • Form handling
  • Pagination
  • Debouncing
  • Local storage access

Your component becomes too large

If a component contains too much logic, extracting it into hooks improves readability.


Logic is unrelated to UI

UI belongs inside components.

Reusable behavior belongs inside hooks.


Where Custom Hooks Are Commonly Used

Custom Hooks are everywhere in modern React applications.


1. API Requests

useFetch()
useUsers()
useProducts()

Enter fullscreen mode Exit fullscreen mode


2. Authentication

useAuth()

Enter fullscreen mode Exit fullscreen mode


3. Theme Handling

useTheme()

Enter fullscreen mode Exit fullscreen mode


4. Form Management

useForm()

Enter fullscreen mode Exit fullscreen mode


5. Window Resize Detection

useWindowSize()

Enter fullscreen mode Exit fullscreen mode


6. Dark Mode

useDarkMode()

Enter fullscreen mode Exit fullscreen mode


7. Local Storage

useLocalStorage()

Enter fullscreen mode Exit fullscreen mode


Example 3: useLocalStorage Hook

Saving values in local storage is common.

Instead of rewriting the logic everywhere, create a reusable hook.

import { useState } from "react";

function useLocalStorage(key, initialValue) {
  const [value, setValue] = useState(() => {
    const storedValue = localStorage.getItem(key);

    return storedValue
      ? JSON.parse(storedValue)
      : initialValue;
  });

  const saveValue = (newValue) => {
    setValue(newValue);

    localStorage.setItem(
      key,
      JSON.stringify(newValue)
    );
  };

  return [value, saveValue];
}

export default useLocalStorage;

Enter fullscreen mode Exit fullscreen mode

Usage:

function App() {
  const [name, setName] = useLocalStorage(
    "username",
    ""
  );

  return (
    <input
      value={name}
      onChange={(e) => setName(e.target.value)}
    />
  );
}

Enter fullscreen mode Exit fullscreen mode


How Custom Hooks Improve Project Structure

Without hooks, components often become messy.

Example of a bad structure:

components/
 ├── Users.jsx
 ├── Products.jsx
 ├── Dashboard.jsx

Enter fullscreen mode Exit fullscreen mode

Each component contains repeated logic.

With hooks:

src/
 ├── components/
 ├── hooks/
 │    ├── useFetch.js
 │    ├── useAuth.js
 │    ├── useLocalStorage.js

Enter fullscreen mode Exit fullscreen mode

Now logic is centralized and reusable.


Important Difference: Component vs Custom Hook

A lot of developers confuse them.

React Component

  • Returns JSX
  • Handles UI rendering
function Button() {
  return <button>Click</button>;
}

Enter fullscreen mode Exit fullscreen mode


Custom Hook

  • Returns logic/data/functions
  • Does NOT return JSX
function useCounter() {
  return { count, increment };
}

Enter fullscreen mode Exit fullscreen mode


Common Mistakes with Custom Hooks

1. Returning JSX from Hooks

Wrong:

function useModal() {
  return <div>Modal</div>;
}

Enter fullscreen mode Exit fullscreen mode

Hooks should return:

  • state
  • functions
  • values
  • logic

Not UI.


2. Creating Hooks Too Early

Not every logic deserves a hook.

If logic is used only once, keep it inside the component.


3. Massive Hooks

Avoid creating giant hooks that do everything.

Bad:

useDashboardEverything()

Enter fullscreen mode Exit fullscreen mode

Keep hooks focused.


4. Ignoring Dependency Arrays

Hooks using useEffect still require proper dependencies.

Wrong dependencies can create bugs.


Advanced Pattern: Hooks Calling Hooks

One powerful feature is that hooks can use other hooks.

Example:

function useAuth() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    // auth logic
  }, []);

  return user;
}

Enter fullscreen mode Exit fullscreen mode

Another hook can reuse it:

function useAdmin() {
  const user = useAuth();

  return user?.role === "admin";
}

Enter fullscreen mode Exit fullscreen mode

This creates highly modular logic.


Real-World Example

Imagine an e-commerce application.

Without hooks:

  • Every page handles fetching
  • Every component manages loading
  • Authentication logic repeats
  • Cart logic duplicates

With hooks:

useProducts()
useCart()
useAuth()
useOrders()
useWishlist()

Enter fullscreen mode Exit fullscreen mode

Everything becomes organized and reusable.


Are Custom Hooks Required?

No.

React applications can work without them.

But as projects grow, Custom Hooks become one of the best ways to keep code maintainable and scalable.

Most modern React applications use them heavily.


Best Practices for Writing Custom Hooks

Keep One Responsibility

Good:

useFetch()

Enter fullscreen mode Exit fullscreen mode

Bad:

useFetchAndThemeAndAuth()

Enter fullscreen mode Exit fullscreen mode


Use Clear Names

Hook names should describe behavior.

Good:

useWindowSize()
useDarkMode()

Enter fullscreen mode Exit fullscreen mode


Return Useful Data

Hooks should expose only what components need.


Handle Errors Properly

Especially in API hooks.


Avoid Unnecessary Re-renders

Use optimization carefully when needed.


Final Thoughts

Custom Hooks are one of React’s cleanest and most practical features.

They help you:

  • Reuse logic
  • Reduce duplication
  • Keep components clean
  • Improve maintainability
  • Build scalable applications

The best way to understand Custom Hooks is to start noticing repeated logic in your own components.

Whenever you find yourself copying the same state management or effect logic multiple times, that’s usually a sign that a Custom Hook could help.

And once you start using them properly, your React code becomes significantly cleaner, easier to manage, and far more reusable.


Conclusion

That’s it for Episode 9 of the “Let’s Master React Hooks Together” series.

In this episode, we explored one of the most practical concepts in React — Custom Hooks — and understood how they help create reusable, clean, and maintainable applications.

We learned:

  • What Custom Hooks are
  • Why React introduced them
  • How to create them
  • When to use them
  • Real-world use cases
  • Best practices and mistakes to avoid

More importantly, we saw how Custom Hooks help separate logic from UI, making React applications much easier to scale and maintain.

As you continue building React projects, you’ll start noticing repeated patterns everywhere. That’s the perfect opportunity to create your own hooks and make your codebase cleaner and smarter.

In the next episode, we’ll continue exploring more advanced React concepts and patterns to strengthen your React development skills step by step.

See you in the next episode of
“Let’s Master React Hooks Together.”