Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement]: Replace array indices with WeakMap for stable React keys #64

Open
zapeture opened this issue Jan 29, 2025 · 1 comment
Open

Comments

@zapeture
Copy link

Description:
Currently, the Block renderer uses array indices as React keys which can cause performance issues during content reordering. We should switch to using WeakMap for stable key generation. There was a FIX ME for this enhancement here and here

Current Behavior:

  • Uses array indices as React keys
  • Uses deprecated substr() method
  • Potential reconciliation issues during content reordering

Proposed Solution:

  • Implement WeakMap to store stable keys for content nodes
  • Replace array index keys with unique node identifiers
  • Update deprecated substr() to slice()

Benefits:

  • Better React reconciliation process
  • Improved performance for dynamic content updates
  • Proper garbage collection of unused nodes

Implementation Notes:

  • Add WeakMap with useRef in Block and BlocksRenderer components
  • Generate unique keys using Math.random()
  • Cache keys for reuse
@zapeture zapeture changed the title [enhancement] Replace array indices with WeakMap for stable React keys [enhancement]: Replace array indices with WeakMap for stable React keys Jan 29, 2025
@monolithed
Copy link

monolithed commented Feb 3, 2025

Could you elaborate on your point about using random keys?
Generating a new key on every render will cause the component to unmount, leading to significant performance issues and unpredictable behavior.
The same goes for using WeakMap with useRef. What performance loss are you referring to with numeric keys? WeakMap only works with objects and doesn’t persist between renders, unlike stable keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants