---
title: React Hooks explained
description: >-
  Hooks let a function component hold state and run side effects. This covers useState, useEffect
  and custom hooks, and why we use as few of them as we can.
url: https://expiredqueues.com/react-hooks-explained/
markdown: https://expiredqueues.com/react-hooks-explained/index.md
date: '2022-02-22T21:23:00.000Z'
image: https://expiredqueues.com/images/og/react-hooks-explained-1b334152.jpg
---

JavaScript posts js

# React Hooks explained

22 February 2022 Updated 12 November 2026 By Adrian Noel 3 min read

On this page

1. [useState](#usestate)
2. [useEffect](#useeffect)
3. [Custom hooks](#custom-hooks)
4. [Use as few as you can](#use-as-few-as-you-can)

Hooks let a function component hold state and run side effects. They arrived in [React 16.8](https://react.dev/reference/react/hooks), and before that you needed a class component for either.

We still use React, and we use as few hooks as we can.

## useState

`useState` takes an initial value. It returns the current state and a function that updates it.

```jsx
import { useState } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
```

## useEffect

`useEffect` runs a function after the component renders. By default it runs after every render, like `componentDidMount` and `componentDidUpdate` in a class component. Pass a dependency array as the second argument to limit when it runs. An empty array runs it once, after the first render.

This component fetches data once and stores it in state:

```jsx
import { useState, useEffect } from 'react';

function Example() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://example.com/api/message')
      .then(response => response.json())
      .then(data => setData(data));
  }, []);

  return (
    <div>
      {data ? <p>{data.message}</p> : <p>Loading...</p>}
    </div>
  );
}
```

## Custom hooks

A custom hook is a function whose name starts with `use` and that calls other hooks. It moves logic out of a component so that other components can share it.

This one fetches data from a URL and returns the data with a loading flag:

```js
import { useState, useEffect } from 'react'

function useFetchData(url) {
  const [data, setData] = useState(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    let ignore = false

    async function fetchData() {
      const response = await fetch(url)
      const json = await response.json()
      if (!ignore) {
        setData(json)
        setLoading(false)
      }
    }
    fetchData()

    return () => {
      ignore = true
    }
  }, [url])

  return { data, loading }
}
```

The function returned from the effect is its cleanup. Here it stops a slow response for an old `url` from overwriting a newer one.

Use the hook in a component like this:

```jsx
import { useFetchData } from './useFetchData'

function Example() {
  const { data, loading } = useFetchData('https://example.com/api/items')

  if (loading) {
    return <p>Loading...</p>
  }

  return (
    <ul>
      {data.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  )
}
```

The component gets `data` and `loading` and doesn't know how they were fetched. You can test `useFetchData` without the component.

## Use as few as you can

The React docs have a page called [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect). If you can calculate a value from props or state during render, calculate it there. Don't store it with `useState` and sync it with `useEffect`.

```jsx
// Not needed
const [fullName, setFullName] = useState('')
useEffect(() => {
  setFullName(firstName + ' ' + lastName)
}, [firstName, lastName])

// Calculate it during render
const fullName = firstName + ' ' + lastName
```

The second version has no extra render and nothing to keep in sync.

### You might be interested in:

- [Top ReactJS Tools of Feb 2019](https://expiredqueues.com/top-reactjs-tools-feb-2019/index.md)
- [Flutter vs. React Native: What You Need to Know](https://expiredqueues.com/flutter-vs-react-native-what-you-need-to-know/index.md)

**Expired Queues is on YouTube.** Short videos on the same topics as this blog.

[Watch on YouTube](https://www.youtube.com/@expiredqueues)

[Previously Awesome Free SVG Icons](https://expiredqueues.com/awesome-best-free-svg-icons/index.md) [Next Flutter vs. React Native: What You Need to Know Before Choosing Your Mobile App Framework](https://expiredqueues.com/flutter-vs-react-native-what-you-need-to-know/index.md)

## Structured data

```json
{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "React Hooks explained",
  "description": "Hooks let a function component hold state and run side effects. This covers useState, useEffect and custom hooks, and why we use as few of them as we can.",
  "datePublished": "2022-02-22T21:23:00.000Z",
  "dateModified": "2026-11-12T00:00:00.000Z",
  "author": {
    "@type": "Person",
    "name": "Adrian Noel",
    "url": "https://expiredqueues.com/about/"
  },
  "publisher": {
    "@type": "Person",
    "name": "Adrian Noel"
  },
  "mainEntityOfPage": "https://expiredqueues.com/react-hooks-explained/",
  "keywords": "posts, js, reactjs, web-development",
  "articleSection": "JavaScript"
}
```
