Skip to main content
Welcome to the React collection of 30 Seconds of Code. This collection contains practical, ready-to-use function components and custom hooks designed for React 18.

What You’ll Find Here

This collection is organized into three main categories:

Hooks

Custom React hooks for state management, side effects, and browser APIs

Components

Reusable UI components for common patterns and interactions

Testing

Testing patterns and examples for React components

Key Features

All examples use modern React patterns including:
  • Function components with hooks
  • Declarative state management
  • Effect cleanup and dependency management
  • Custom hook composition
Each snippet is designed to be:
  • Copy-paste ready for your projects
  • Well-documented with explanations
  • Tested and validated
  • Following React best practices
Every snippet includes:
  • Complete implementation code
  • Usage examples
  • Edge case handling
  • Performance considerations

React 18 Focus

This collection specifically targets React 18, taking advantage of:
  • Concurrent rendering features
  • Automatic batching
  • Modern hooks API
  • Updated effect behavior

Quick Start

Here’s a simple example of a custom hook from this collection:
const useToggle = (initialState = false) => {
  const [state, setState] = React.useState(initialState);
  const toggle = React.useCallback(() => setState(prev => !prev), []);
  return [state, toggle];
};

// Usage
const App = () => {
  const [isOpen, toggleOpen] = useToggle(false);
  return <button onClick={toggleOpen}>{isOpen ? 'Close' : 'Open'}</button>;
};
1

Explore Hooks

Start with the Hooks Overview to discover custom hooks for managing state, effects, and browser APIs.
2

Browse Components

Check out the Components Overview for ready-to-use UI components.
3

Learn Testing

Visit the Testing Overview to learn how to test React components effectively.

Categories at a Glance

Custom Hooks

  • Effect Hooks: useInterval, useTimeout, useEffectOnce
  • Event Hooks: useClickOutside, useClickInside
  • Network Hooks: useFetch
  • Observer Hooks: useMutationObserver, useIntersectionObserver
  • Lifecycle Hooks: useComponentDidMount, useComponentDidUpdate

UI Components

  • Interactive: Toggle, Star Rating, Modal
  • Data Display: DataTable, TreeView, DataList
  • Feedback: Tooltip, Alert, Modal Dialog

Testing Patterns

  • Asynchronous component testing
  • Redux connected components
  • Portal testing
  • Event simulation
All code examples in this collection use modern JavaScript syntax and assume you have a React 18 environment set up.

Build docs developers (and LLMs) love