Skip to content

Conversation

@ibrahimcesar
Copy link
Owner

Summary

Fixes a critical infinite rendering loop in the Events Demo component that was causing:

  • Infinite "onIframeAdded" events flooding the event log
  • Player state not updating correctly
  • Poor demo performance and user experience

Problem

The Events Demo was experiencing an infinite rendering loop caused by inline event handler functions being recreated on every render:

  1. Inline arrow functions like onIframeAdded={() => addEvent('onIframeAdded')} created new function references on each render
  2. React's useCallback in the library component detected these as prop changes
  3. This triggered the useEffect dependency to re-run, calling onIframeAdded()
  4. Which called addEvent() → updated state via setEvents() → re-rendered the component
  5. Back to step 1 → infinite loop!

User Impact:

  • Event log showed endless stream of onIframeAdded events instead of single event
  • Player State box remained stuck on "Not Started" and never updated
  • Confusing demonstration of the events feature
  • Poor performance due to constant re-rendering

Solution

Wrapped all event handlers and helper functions in useCallback to maintain stable function references across renders:

  • ✅ Memoized addEvent helper function
  • ✅ Memoized clearEvents helper function
  • ✅ Memoized all 10 event handlers:
    • handleIframeAdded
    • handleReady
    • handleStateChange
    • handlePlay
    • handlePause
    • handleEnd
    • handleBuffering
    • handleError
    • handlePlaybackRateChange
    • handlePlaybackQualityChange

Additional Improvements:

  • Moved PLAYER_STATE_NAMES constant outside component (performance optimization)
  • Updated demo description to clarify events are scoped to specific video embed
  • Updated demo to use local library build (file:..) for consistency

Changes

Modified Files

  • demo/pages/index.js

    • Added useCallback import from React
    • Wrapped all event handlers in useCallback hooks
    • Moved state names constant outside component
    • Improved demo description text
  • demo/package.json & demo/package-lock.json

Testing

All 51 unit tests pass

npm test
# Test Files  1 passed (1)
# Tests  51 passed (51)
✅ Library builds successfully

npm run build
# ✓ built in 2.37s
✅ Demo builds successfully

cd demo && npm run build  
# ✓ Compiled successfully
# ✓ Generating static pages (3/3)

Breaking Changes
None. This is a demo-only fix with no changes to the library API.

Fixed an infinite rendering loop in the demo's EventsExample component
caused by inline event handler functions being recreated on every render.

## Problem

The inline arrow functions passed to LiteYouTubeEmbed event handlers
(e.g., `onIframeAdded={() => addEvent('onIframeAdded')}`) were creating
new function references on every render. This caused:

1. New function reference created on each render
2. useCallback in the library detects prop change
3. useEffect re-runs and calls onIframeAdded()
4. addEvent() updates state via setEvents()
5. Component re-renders → back to step 1 (infinite loop)

This resulted in:
- Infinite "onIframeAdded" events appearing in the log
- Poor demo performance
- Player state not updating correctly due to render chaos

## Solution

Wrapped all event handlers and helper functions in useCallback:
- `addEvent` - memoized to prevent recreation
- `clearEvents` - memoized to prevent recreation
- All 10 event handlers (handleIframeAdded, handleReady, etc.)

Additional improvements:
- Moved PLAYER_STATE_NAMES constant outside component
- Updated description to clarify events are scoped to specific embed
- Ensured player state updates work correctly

## Testing

✅ All 51 tests pass
✅ Library builds successfully
✅ Demo builds successfully without errors
✅ No breaking changes - fully backward compatible

Co-authored-by: Claude <[email protected]>
@ibrahimcesar ibrahimcesar added this to the 3.0.0 milestone Nov 15, 2025
@ibrahimcesar ibrahimcesar self-assigned this Nov 15, 2025
@ibrahimcesar ibrahimcesar added documentation Improvements or additions to documentation enhancement New feature or request javascript Pull requests that update javascript code labels Nov 15, 2025
@ibrahimcesar ibrahimcesar merged commit 2ccbc58 into main Nov 15, 2025
5 checks passed
@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
ES Module 2.88 KB (0%)
CommonJS 2.84 KB (0%)
CSS 1.05 KB (0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request javascript Pull requests that update javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants