shadcn-ahooks

useTrackedEffect

A hook of useEffect that allow us to track which dependencies caused the effect to trigger.

Overview

A hook of useEffect that allow us to track which dependencies caused the effect to trigger.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useTrackedEffect.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useTrackedEffect.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useTrackedEffect.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useTrackedEffect.json

A hook of useEffect that allow us to track which dependencies caused the effect to trigger.

Examples

Basic usage

API

useTrackedEffect(
  effect: (changes: [], previousDeps: [], currentDeps: []) => (void | (() => void | undefined)),
  deps?: deps,
)

The API is alike React.useEffect, but the first function will receive three parameters: changes, previousDeps, and currentDeps.

  • changes: Index of changed dependencies
  • previousDeps: Last deps
  • currentDeps: Current deps

On this page