Code Explainers

Code explainers tagged #custom-hooks

javascript
import { useState, useEffect, useCallback, useRef } from "react";
 
export function usePersistentForm(storageKey, initialValues) {
  const [values, setValues] = useState(() => {

A React hook that persists form state to localStorage

custom-hooks localstorage debounce
Intermediate 9 steps
javascript
import { useCallback, useEffect, useRef, useState } from 'react';
 
export function useInfiniteScroll(fetchPage) {
  const [items, setItems] = useState([]);

Building an infinite scroll hook in React

custom-hooks intersection-observer pagination
Advanced 6 steps
javascript
import { useState, useEffect, useMemo } from 'react';
 
function useDebounce(value, delay = 300) {
  const [debounced, setDebounced] = useState(value);

Debounced search with cancellation in React

custom-hooks debounce abortcontroller
Intermediate 9 steps
javascript
import { useState, useEffect } from "react";
 
export function useUser(userId) {
  const [user, setUser] = useState(null);

How a data-fetching hook works in React

custom-hooks data-fetching abortcontroller
Intermediate 8 steps
typescript
import { useState, useEffect, useRef, useCallback } from "react";
 
interface SearchResult {
  id: string;

A debounced search hook in React

debounce custom-hooks abortcontroller
Intermediate 7 steps