event-delegation

Implement Event Delegation in React with a single parent handler and data-action attributes.

Updated May 20, 2025
One-click install
npx skills add https://github.com/trae-op/electron-password-generation --skill event-delegation
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: event-delegation
Source: https://github.com/trae-op/electron-password-generation/tree/main/.github/skills/event-delegation
Command: npx skills add https://github.com/trae-op/electron-password-generation --skill event-delegation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Event delegation reduces prop drilling and the need to attach handlers to every child by placing a single listener on a parent container, using event.target to identify the source component.

Core Features & Use Cases

  • Single parent event listener that handles actions for many child elements.
  • Reduces prop drilling across deeply nested components and improves performance.
  • Ideal for dynamic lists/grids where items are created or removed at runtime and each item supports actions via data attributes.

Quick Start

Attach a single onClick handler to a parent container and mark actionable elements with data-action attributes. In the handler, use closest('[data-action]') to identify the action and optionally read data-id for item identity, then dispatch the appropriate function.

Frequently Asked Questions about event-delegation

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I avoid prop drilling when handling clicks in a dynamic React list?

Event delegation in React avoids prop drilling by attaching a single onClick listener to a parent container and using event.target with closest('[data-action]') to identify clicked child elements and dispatch responses.

What is the best way to manage event listeners for many items in a React grid?

The best way to manage event listeners for a React grid is event delegation, which places one handler on the parent container and uses data attributes on actionable elements to identify and execute the correct actions.

How do I use data attributes to handle actions in a React event delegation pattern?

To use data attributes for event delegation in React, mark actionable elements with data-action and optionally data-id for item identity, then read these attributes inside the parent handler using event.target.closest('[data-action]').

Does event delegation improve React performance for deeply nested components?

Yes, event delegation improves React performance for deeply nested components by reducing event listener overhead and minimizing prop drilling, requiring only a single parent handler instead of attaching handlers to every child element.

When should I not use event delegation in React UIs?

You should avoid event delegation in React UIs when actions require complex event capture or non-bubbling events, as the pattern relies entirely on event bubbling and closest() traversal to match data-action attributes from event.target.

Can I handle dynamic runtime elements with a single event handler in React?

Yes, you can handle dynamic runtime elements with a single event handler in React using event delegation, which automatically captures clicks from newly created or removed list items without attaching new listeners.