What Are React Hooks? Explained for Beginners
What are React hooks? Learn what hooks are, why React has them, the two golden rules, and how to use useState and useEffect with simple, runnable examples.
What are React hooks? Learn what hooks are, why React has them, the two golden rules, and how to use useState and useEffect with simple, runnable examples.
If you are searching for "what are React hooks", here is the short answer: a hook is a special function that lets a React component use features like memory (state) and side effects. Hooks always start with the word "use", such as useState and useEffect. In this guide you will learn what hooks are, why they exist, the two rules you must follow, and how to try them in small examples.
You do not need any earlier React knowledge beyond knowing what a component is. Every new word is explained the first time we use it.
A React component is a function that returns some HTML-like code (called JSX) to show on the screen. By default, that function is very simple. It runs, returns the screen, and forgets everything. It cannot remember a number a user clicked, and it cannot load data from a server.
Hooks fix this. A hook lets you "hook into" React's built-in features from inside your function. Think of a hook as a plug that connects your plain function to a power socket that React provides.
Before hooks (before 2019), a component that needed memory had to be written as a class, which is a longer and harder style of code. Hooks let you do the same thing with a short, plain function. Today, almost all new React code uses function components with hooks.
| Without hooks | With hooks |
|---|---|
| Needs a class, this, and lifecycle methods | Just a function |
| Related code is split across many methods | Related code sits together |
| Hard for beginners to read | Short and easy to read |
State is data that a component remembers and that can change. When state changes, React redraws the component. Here is a counter.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
You clicked {count} times
</button>
);
}
export default Counter;Line by line: the first line imports the hook from React. Inside Counter, useState(0) creates a piece of state that starts at 0. It returns two things: the current value (count) and a function to change it (setCount). We grab both using array destructuring, which is just a short way to name the two items. In the button, each click calls setCount with count + 1. React then runs Counter again and shows the new number.
Want more detail on this one? Read our full guide to the useState hook, linked below.
Go deeper on the most-used hook with our step-by-step useState guide.
Read the useState guideA side effect is anything a component does besides drawing the screen. Examples are changing the browser tab title, starting a timer, or fetching data. You put side effects inside useEffect.
import { useState, useEffect } from 'react';
function TitleCounter() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = 'Clicks: ' + count;
}, [count]);
return (
<button onClick={() => setCount(count + 1)}>
Click me ({count})
</button>
);
}
export default TitleCounter;The function inside useEffect runs after React draws the component. Here it sets the browser tab title to show the count. The array at the end, [count], is called the dependency array. It tells React: "only run this effect again when count changes". If you pass an empty array [], the effect runs just once, when the component first appears.
React needs hooks to be called in the same order every time a component runs. That is how it matches each hook to its stored value. To keep this true, there are two simple rules.
Here is a common mistake and its fix.
// Wrong: the hook is inside an if
function Profile({ loggedIn }) {
if (loggedIn) {
const [name, setName] = useState('');
}
return <p>Hello</p>;
}
// Right: the hook is at the top level
function Profile({ loggedIn }) {
const [name, setName] = useState('');
if (!loggedIn) {
return <p>Please log in</p>;
}
return <p>Hello {name}</p>;
}In the wrong version, the hook might run on one render and be skipped on the next, which confuses React. In the right version, useState is always called first, and the if comes after. If you need a condition, put it below your hooks or inside the hook's function.
A custom hook is your own function whose name starts with "use" and that calls other hooks. It lets you reuse logic in many components. This one keeps a toggle (on or off) value.
import { useState } from 'react';
function useToggle(start = false) {
const [on, setOn] = useState(start);
const toggle = () => setOn(!on);
return [on, toggle];
}
function LightSwitch() {
const [on, toggle] = useToggle();
return (
<button onClick={toggle}>
The light is {on ? 'on' : 'off'}
</button>
);
}useToggle stores a true/false state and returns the current value plus a function to flip it. The default start = false means it begins as off if you pass nothing. LightSwitch calls the hook and uses the result. The text uses a ternary, a short if/else written as condition ? a : b. Any component can now call useToggle without repeating the code.
Now that you know what hooks are, practise with useState and useEffect in small projects, like a counter, a to-do list, or a simple timer. Once those feel natural, look at useRef and useContext. Take it one hook at a time.
Hooks are special functions that let a React function component use features such as memory (state) and side effects. Their names start with "use", like useState and useEffect. They replaced the older, longer class-based style.
No. Hooks only work in function components and in custom hooks. If you have an old class component, you must rewrite it as a function to use hooks.
React tracks hooks by the order they are called. If a hook is sometimes skipped, the order changes between renders and React mixes up the stored values. Keep hooks at the top level of the component.
useState remembers a value and redraws the component when the value changes. useEffect runs extra work, like loading data or changing the page title, after the component is drawn.
React ships with about fifteen built-in hooks, but beginners only need useState and useEffect at first. You can also write your own custom hooks.
Learning React and want an expert to build or review your project? Our team can help.
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.