Skip to content
BENIBIX Learn

React Hooks

Development🌐 Web DevelopmentFrameworks

Also known as Hooks (React), useState, useEffect, React Hook

Small React tools whose names start with use. useState lets a part of the page keep a value.

Think of it like

It is like a toy with snap-on parts. Snap one on, and the toy can do a new trick.

Example

Jack's Like button must count clicks. He adds useState(0), and each click calls setLikes to raise the number on screen.

Why it matters

Every modern React component uses them. They are how a component holds data and reacts to change.

Definition

React Hooks are functions that let a React component use React's features. Their names start with use. useState lets a component remember a value, like a count. useEffect runs code after the page shows, like fetching data. You can also build your own hooks from these. Hooks must be called at the top of a component, never inside an if or a loop.

Example code

jsx
const [likes, setLikes] = useState(0);
return <button onClick={() => setLikes(likes + 1)}>{likes}</button>;
useState gives a value and a way to change it. Each click shows the new count.
A hook, setCount, changes state. React then updates only what changed.

Common mistake

Calling a hook inside an if. React counts on hooks being called in the same order each time.

Key takeaways

  1. 1
    Names start with use. useState, useEffect, useRef.
  2. 2
    Top level only. Never inside an if or a loop.
  3. 3
    Make your own. Combine hooks into a new one.