R

React Handbook

Clean • Professional

Specialized React Hooks – Advanced Usage

2 minute

Specialized React Hooks

React provides several specialized hooks beyond the common useState, useEffect, and useContext. These hooks are used in advanced scenarios where you need precise control over DOM, component instances, or debugging custom hooks.

The main specialized hooks are:

learn code with durgesh images

  1. useLayoutEffect – Synchronous side effects
  2. useImperativeHandle – Controlling ref exposure
  3. useDebugValue – Debugging custom hooks

useLayoutEffect – Synchronous Side Effects

useLayoutEffect is similar to useEffect but runs synchronously after all DOM mutations and before the browser paints the UI.

When to Use

  • Measuring DOM elements immediately after render
  • Updating layout or styles based on DOM measurements
  • Preventing visual flickers

Syntax:

useLayoutEffect(() => {
  // Your code here
  return () => {
    // Cleanup
  };
}, [dependencies]);

Example:

import React, { useRef, useLayoutEffect, useState } from "react";

function Box() {
  const boxRef = useRef();
  const [width, setWidth] = useState(0);

  useLayoutEffect(() => {
    setWidth(boxRef.current.offsetWidth);
  }, []);

  return (
    <div>
      <div ref={boxRef} style={{ width: "50%" }}>Resizable Box</div>
      <p>Box width: {width}px</p>
    </div>
  );
}

useImperativeHandle – Controlling Ref Exposure

useImperativeHandle works with forwardRef to customize what values or methods are exposed to a parent component via a ref.

When to Use

  • Exposing child component methods
  • Hiding internal DOM implementation details
  • Controlling interaction with parent components

Syntax:

useImperativeHandle(ref, () => ({
  method1,
  method2
}));

Example:

import React, { useRef, forwardRef, useImperativeHandle } from "react";

const CustomInput = forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current.focus()
  }));

  return <input ref={inputRef} />;
});

function App() {
  const inputRef = useRef();

  return (
    <div>
      <CustomInput ref={inputRef} />
      <button onClick={() => inputRef.current.focus()}>Focus Input</button>
    </div>
  );
}

useDebugValue – Debugging Custom Hooks

useDebugValue allows displaying debug information for custom hooks in React DevTools. It does not affect runtime behavior but helps developers debug hooks easily.

Syntax:

useDebugValue(value);

Example:

import { useState, useEffect, useDebugValue } from "react";

function useOnlineStatus() {
  const [isOnline, setIsOnline] = useState(navigator.onLine);

  useEffect(() => {
    const handleOnline = () => setIsOnline(true);
    const handleOffline = () => setIsOnline(false);

    window.addEventListener("online", handleOnline);
    window.addEventListener("offline", handleOffline);

    return () => {
      window.removeEventListener("online", handleOnline);
      window.removeEventListener("offline", handleOffline);
    };
  }, []);

  useDebugValue(isOnline ? "Online" : "Offline");

  return isOnline;
}

Summary of Specialized Hooks

HookPurposeTypical Use Case
useLayoutEffectRun synchronous side effects after DOM mutationsMeasuring DOM, layout adjustments, avoiding flicker
useImperativeHandleExpose specific methods or properties to parent via refChild component APIs, encapsulation
useDebugValueDisplay debug info for custom hooks in DevToolsDebugging complex custom hooks

 

Article 0 of 0