React Hooks: From âWhat the Hook?!â to Hooked for Life đŁđ
When I first heard about React hooks, my brain was like, âHooks? Are we fishing in the code?â đŁ But little did I know, these hooks would reel me into the world of React and never let go. This is the story of my love-hate relationship with three of the most iconic hooks: useState
, useRef
, and useEffect
. Letâs dive in and see how these buddies changed the way I codeâforever.
Act 1: useState
- The Rollercoaster of Emotions đą
The first hook I ever met was useState
, and let me tell youâitâs like your emotional support system in React. Youâre building an app, and suddenly you need to keep track of things like counters, toggles, or user inputs. Boom! In comes useState
.
Hereâs how I started:
const [count, setCount] = useState(0);
I remember staring at this line for 15 minutes straight. âWhy are we destructuring? Where is this setCount
coming from?!â It felt like React was speaking a language only aliens understood. đž
But once I figured it out, I felt like Iâd unlocked a superpower. Stateful components? No more class components? Just a single function call? đ€Ż
useState
was my first crush, and letâs be honestâitâs still my favorite. You want to toggle dark mode? Boom. Update a form value? Boom. You want to build a counter app just for the dopamine hit of watching the number go up? BAM!
const [mood, setMood] = useState("đ");
// Toggle mood
const toggleMood = () => setMood(mood === "đ" ? "đ" : "đ");
Itâs like magicâbut youâre the magician. đ©âš
Act 2: useRef
- The Silent Observer đ
Next up was useRef
, and Iâll be honestâthis one didnât make sense at first. People told me, âItâs used for accessing DOM elements or storing mutable values.â
Me? I was like, âWhy canât I just use state for that?â
Then I tried to manage a timer using useState
. Spoiler alert: It was a disaster. The state kept re-rendering, and I felt like I was stuck in an infinite loop of despair. Thatâs when useRef
swooped in like a hero in a Tamil movie climax. đ„đ„
const timerRef = useRef(null);
const startTimer = () => {
timerRef.current = setInterval(() => {
console.log("Tick-tock â°");
}, 1000);
};
const stopTimer = () => {
clearInterval(timerRef.current);
timerRef.current = null;
};
This was the moment I realized useRef
isnât just a random sidekickâitâs the quiet MVP. It doesnât cause re-renders. Itâs like the backstage crew of your app, making sure everything runs smoothly without stealing the spotlight.
Act 3: useEffect
- The Overachiever đ
Ah, useEffect
âthe one hook that made me question all my life choices. Everyone said, âItâs for side effects.â I was like, âWhat even is a side effect?â
For the first few weeks, I wrote useEffect
like this:
useEffect(() => {
console.log("Component rendered!");
});
And then I kept wondering why my app behaved like it had a caffeine overdoseâre-rendering endlessly. "Why React, why?" đ
Thatâs when I learned about dependencies:
useEffect(() => {
console.log("This only runs when count changes!");
}, [count]);
It clicked. Suddenly, useEffect
went from annoying frenemy to lifesaver. Need to fetch data? useEffect
. Clean up event listeners? useEffect
. Run a side effect when something changes? USE. EFFECT.
And letâs not forget the cleanup functionâitâs like the nice friend who makes sure the party is over before everyone starts wrecking your house.
useEffect(() => {
const handleScroll = () => console.log("Scrolling!");
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
Itâs like having a bouncer for your events. No mess, no drama. đȘ
How I Really Understood Hooks
At first, hooks felt like riddles wrapped in mysteries. But the more I used them, the more I realized theyâre just toolsâpowerful tools that let you write clean, efficient React apps.
useState
: When you need to manage state. Itâs the bread and butter of React.useRef
: When you need a mutable reference or access to the DOM without re-rendering.useEffect
: When you need to handle side effects like data fetching, subscriptions, or anything async.
Where Hooks Shine
Hooks are everywhere in React:
- Forms: Managing input fields with
useState
. - Timers: Using
useRef
to store interval IDs. - APIs: Fetching data with
useEffect
and cleaning up subscriptions. - Custom Hooks: Combine logic into reusable hooks.
They make your code simpler, cleaner, and more readableâlike upgrading from a Nokia 1100 to an iPhone.
Final Words
If React hooks seem confusing, donât worryâyouâre not alone. They take time, but once you get it, youâll wonder how you ever lived without them.
And remember: Hooks arenât just a featureâtheyâre a philosophy. They make your components simpler, your code cleaner, and your life easier. đ
So, if you havenât embraced hooks yet, what are you waiting for? Go on, get hooked! đ