Hey buddy! 👋
Ever wished your simple React components could do more — like remember things, run code at just the right time, or even share data across the app?
React Hooks are the magical tools that make it happen! 🧙♂️✨
Today, I’ll walk you through Hooks in a super simple and fun way. Let’s dive in! 🏊
🚀 What Are Hooks?
Hooks are functions that let you “hook into” React features from functional components.
Before Hooks, we had to use class components to manage state or lifecycle methods.
But now?
🪄 With Hooks, even simple functions can be smart!
🧠 Why Hooks Are Important
Here’s why everyone loves Hooks:
- 🧩 Reuse logic easily
- 🧼 Cleaner, smaller code
- 💡 Work only with functional components
- 🔄 Avoid class confusion (
this
, binding, etc.)
🟢 Meet the Star Hooks
Hook | What It Does |
---|---|
useState | Lets your component remember things |
useEffect | Runs code at specific times |
useContext | Shares data across components |
useReducer | Manages complex state like Redux |
useCallback | Remembers a function |
useMemo | Remembers a calculated value |
Let’s explore each one with baby steps 🐾
🟨 useState
– Remember Stuff in Your App 🧠
jsxCopyEditimport React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click Me!</button>
</>
);
}
🔍 What’s Happening?
useState(0)
gives us:count
: the current valuesetCount
: a function to update it
- When you click the button,
setCount
updates the value. - React re-renders the component.
🤓 Fun fact: State updates cause re-rendering!
🟩 useEffect
– Do Stuff After Render 🔁
Want to fetch data, set timers, or clean things up?
Use useEffect
!
jsxCopyEditimport React, { useState, useEffect } from "react";
function Timer() {
const [time, setTime] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setTime((t) => t + 1);
}, 1000);
return () => clearInterval(interval); // Cleanup
}, []);
return <p>⏱ Time: {time}s</p>;
}
🧪 What’s Going On?
useEffect
runs after the component renders.- It sets a timer, and cleans it when the component is removed.
🟦 useContext
– Share Data Without Props 📨
Sometimes we want to pass data from parent to child to grandchild.
Props are fine, but messy. 😵💫
With useContext
, we skip the chain.
jsxCopyEditconst UserContext = React.createContext();
function App() {
return (
<UserContext.Provider value="Vikas 🚀">
<Profile />
</UserContext.Provider>
);
}
function Profile() {
const user = React.useContext(UserContext);
return <p>Hello, {user}!</p>;
}
🚀 No need to pass user
through props manually!
🟥 useReducer
– When useState Gets Complicated ⚙️
If your state has many conditions, use useReducer
.
It works like Redux — with actions and reducers.
jsxCopyEditconst reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
default:
return state;
}
};
function Counter() {
const [state, dispatch] = React.useReducer(reducer, { count: 0 });
return (
<>
<p>{state.count}</p>
<button onClick={() => dispatch({ type: "INCREMENT" })}>+</button>
</>
);
}
📦 Think of dispatch
as sending commands!
🟧 useCallback
– Don’t Recreate Functions Every Time 🔁
Helps avoid unnecessary re-renders by memorizing a function.
jsxCopyEditconst memoizedFn = useCallback(() => {
// logic here
}, [dependencies]);
Use it when you pass functions to child components that shouldn’t change unless needed.
🟪 useMemo
– Don’t Recalculate Unnecessarily 🧮
Just like useCallback
, but for values, not functions.
jsxCopyEditconst result = useMemo(() => computeExpensiveValue(x), [x]);
This saves time when a calculation is slow.
🔥 Best Practices for Using Hooks
✅ Always use Hooks at the top level of your component.
❌ Don’t call them inside loops, ifs, or nested functions.
📦 Keep custom logic in custom hooks (like useAuth
, useCart
, etc.).
🧪 Combine useReducer
+ useContext
for global state.
⚠️ Use useMemo
and useCallback
only when needed (don’t over-optimize).
📊 Infographic: Which Hook to Use?

A chart showing:
Need | Hook |
---|---|
Basic state | useState |
Side effects like API calls | useEffect |
Global/shared data | useContext |
Complex state logic | useReducer |
Optimize functions | useCallback |
Optimize values | useMemo |
🧾 Summary – React Hooks in a Nutshell
Hook | Use For |
---|---|
useState | Store values like counters, text |
useEffect | Side effects like API calls |
useContext | Sharing global data (like auth) |
useReducer | Complex logic/state transitions |
useCallback | Prevent re-creating functions |
useMemo | Prevent re-calculating expensive values |
👨🏫 Final Thoughts
React Hooks make functional components powerful 💪.
They help you write clean, organized, and reusable code.
Use them wisely and you’ll build better apps in less time 🚀
🙌 Keep Learning with Web Codder
👉 Want more React tips like this?
🎥 Subscribe & Follow Web Codder:
- 📺 YouTube: Click to Subscribe
- 📸 Instagram: Follow Us
- 💬 WhatsApp Community: Join Here
We’re building web magic one line of code at a time! 🧙♂️✨