Skip to content
AI & Agents
Agent

advanced-event-handler-refs

Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.

From plugin
caldiy
50k95 skills95 agents

Claude Code

$ npx -y skills add calcom/cal.diy --agent claude-code

Agent definition

advanced-event-handler-refs.md
title: Store Event Handlers in Refs
impact: LOW
impactDescription: stable subscriptions
tags: advanced, hooks, refs, event-handlers, optimization

Store Event Handlers in Refs

Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.

**Incorrect (re-subscribes on every render):**

function useWindowEvent(event: string, handler: (e: Event) => void) {
  useEffect(() => {
    window.addEventListener(event, handler)
    return () => window.removeEventListener(event, handler)
  }, [event, handler])
}

**Correct (stable subscription):**

function useWindowEvent(event: string, handler: (e: Event) => void) {
  const handlerRef = useRef(handler)
  useEffect(() => {
    handlerRef.current = handler
  }, [handler])

  useEffect(() => {
    const listener = (e: Event) => handlerRef.current(e)
    window.addEventListener(event, listener)
    return () => window.removeEventListener(event, listener)
  }, [event])
}

**Alternative: use `useEffectEvent` if you're on latest React:**

import { useEffectEvent } from 'react'

function useWindowEvent(event: string, handler: () => void) {
  const onEvent = useEffectEvent(handler)

  useEffect(() => {
    window.addEventListener(event, onEvent)
    return () => window.removeEventListener(event, onEvent)
  }, [event])
}

`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.

Read more
@calcom
Ships withcaldiy

Scheduling infrastructure for absolutely everyone.

50k3 skills95 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
50.2k
Star history of calcom/cal.com on GitHub, a chart by star-history.com

Other agents on caldiy.

knowledge-base

knowledge-base

Agent

This file contains domain knowledge about the Cal.diy product and codebase. For coding guidelines and rules, see…

@calcom@calcomInstall Agent
architecture-features-modules

architecture-features-…

Agent

The `packages/features` package should contain only framework-agnostic code: - Repositories (data access layer) - Services…

@calcom@calcomInstall Agent

More agents from other plugins.

Argon

Argon

Agent

Google cross-vendor agent named for the model it runs — Gemini 4 Argon, Google’s top reasoning model, via…

lifeos3w20k
@danielmiessler@danielmiesslerInstall Agent
a2a

a2a

Agent

Delegate tasks to another AI expert via Yao A2A protocol. Use when the user @mentions an expert.

yao4w8.2k
@yaoapp@yaoappInstall Agent

Questions about advanced-event-handler-refs.

Which coding agents can use advanced-event-handler-refs?
advanced-event-handler-refs installs in Claude Code and Cursor: each loads subagents from .claude/agents, so copy the file there. Flowy's auto-invocation runs in Claude Code only.