Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
React Hooks let function components use state, context, refs, Effects, and other React features. Start with useState when a component needs to remember changing information; use other Hooks for distinct needs such as reading ancestor-provided context, holding a DOM reference, or synchronizing with an external system. Call Hooks at the top level of a component or custom Hook, and use Effects only when synchronization with something outside React is needed.
What are React Hooks?
Hooks are functions that let function components use React features. Built-in Hooks handle needs such as state, context, refs, Effects, and performance-related work. A custom Hook can combine Hooks to package reusable behavior.
The most useful way to learn Hooks is by the problem each solves, rather than by memorizing a flat list. For a component that needs to remember changing information, begin with useState.
How do I keep changing information in component state?
useState gives a component a state value and a setter function. When the setter is called, React requests an update and renders the component again with the new state.
#1 Best Overall
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<button onClick={handleClick}>
Count: {count}
</button>
);
}
The count variable belongs to the render currently running. Calling setCount does not change that variable immediately inside the same event handler; it requests a later render, where the component receives the updated value.
When should I use context or a ref?
Context and refs serve different needs from state. Use context to read information made available by an ancestor. Use a ref to hold a value that should persist between renders but is not itself used to render the UI, such as a DOM node or timeout ID.
- Context: read a value provided higher in the component tree.
- Ref: retain a value or DOM reference without asking React to render again when it changes.
Updating a ref does not trigger a re-render. If a changed value needs to update what the component displays, state is usually the relevant tool instead.
When should I use useEffect?
Use useEffect to synchronize a component with an external system, such as a chat connection, browser API, or other system outside React. An Effect’s setup can return a cleanup function that undoes or disconnects what setup established.
Rank #3
import { useEffect } from 'react';
function ChatRoom({ roomId }) {
useEffect(() => {
const connection = createConnection(roomId);
connection.connect();
return () => {
connection.disconnect();
};
}, [roomId]);
return <h1>Chat room: {roomId}</h1>;
}
Here, setup connects to the room identified by roomId, and cleanup disconnects that connection. The dependency list reflects the reactive values the synchronization reads. When a dependency changes, React can clean up the previous synchronization and set up the new one. It is not a general-purpose scheduling control.
When an Effect is unnecessary
If there is no external system to synchronize with, an Effect may not be needed. Avoid using Effects as the default place for calculations or ordinary data flow; they are an escape hatch for synchronization. Fetching directly in Effects can also lead to repeated boilerplate and make caching or server rendering harder. React’s useEffect documentation points readers toward custom Hooks or community data-fetching solutions for those needs.
Rank #4
Why does Strict Mode run an Effect an extra time?
In development, React Strict Mode runs an additional setup-and-cleanup cycle to help reveal Effects whose cleanup does not mirror their setup. This is intentional. Make setup and cleanup correctly pair rather than suppressing the extra cycle as a first response. See React’s Effect guidance.
Why can’t I call a Hook conditionally?
Call ordinary Hooks at the top level of a function component or custom Hook, before any early return. Do not call them inside conditions, loops, nested callbacks, event handlers, or regular JavaScript functions. React relies on Hooks being called in the same order on each render; changing that order can make it associate state with the wrong Hook.
Best Value
The special React use API has different conditional-call rules from ordinary Hooks. Do not generalize its exception to other Hooks. React documents the distinction in its Rules of Hooks lint guidance.
How do I write a custom Hook?
A custom Hook packages reusable behavior so multiple components can use the same logic. Its name should conventionally begin with use followed by a capital letter, and it may call other Hooks. For example, a useChatRoom Hook could own the connection Effect for a room and let components reuse that behavior.
A custom Hook is not a way to make built-in Hook calls conditional. It follows the same top-level calling rule: call the custom Hook from a component or another custom Hook, not from an event handler or an arbitrary JavaScript function. See React’s guide to reusing logic with custom Hooks.
How can I catch Hook mistakes while coding?
Enable React’s Hooks lint rules in your development setup. They can flag common call-order violations as you write, before those mistakes become harder to trace. React recommends enabling all React Hooks rules.
- Confirm every ordinary Hook is called at the top level of a function component or custom Hook.
- Check that the Hook is not inside a condition, loop, nested callback, event handler, or regular JavaScript function.
- For an Effect, check that its dependencies match the reactive values used by its synchronization logic and that cleanup undoes setup when needed.
- Use an editor with React and JavaScript support and ESLint integration that can show the Hooks rules. React’s editor guidance covers VS Code, WebStorm, Sublime Text, and Vim; choose according to your preference and available lint support.
React’s current references include Rules of Hooks, built-in React Hooks, useState, and the React Hooks lint plugin.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

