πͺ 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 Code | No need to write classes, constructors, or bind methods |
Easier to Read | Functions are smaller and cleaner |
Reusable Logic | You can make custom hooks to share logic between components |
Recommended | React 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
Hook | Use For |
---|---|
useState | Add and update data |
useEffect | Run code after render (side effects) |
useRef | Keep values or get DOM elements |
useContext | Use shared/global data |
Custom Hook | Reuse 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.
Comments are closed.