fix(table): read table element from context to prevent stale-path crash on Enter (#5064) - #5072
Closed
Samarth1306w wants to merge 1 commit into
Closed
fix(table): read table element from context to prevent stale-path crash on Enter (#5064)#5072Samarth1306w wants to merge 1 commit into
Samarth1306w wants to merge 1 commit into
Conversation
…sh on Enter (#5064) When Enter splits or inserts a block before a table, useElementSelector's path becomes stale and resolves to the wrong node (e.g. the heading). Passing this non-table node to compileTableGrid crashes with: TypeError: Cannot read properties of undefined (reading 'forEach') Fix: useTableColSizes now reads the table element from useElement(KEYS.table) which always returns the context-bound table node, instead of re-reading from the editor via a potentially stale path. Closes #5064
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
🦋 Changeset detectedLatest commit: 6611d53 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5064 — Homepage editor throws from table grid code when pressing Enter on
next.Root Cause
useTableColSizesreads the table path viauseElementSelectorand then re-reads the table node from the editor using that path. During a transaction that inserts or splits a block before the table (e.g. pressing Enter in a heading above the table), the path becomes stale and resolves to the wrong node (the newly inserted heading). This non-table node gets passed intocompileTableGrid→compileTableElement, which expects table rows but receives heading children, causing:Fix
Replace the stale-path re-read pattern with
useElement<TTableElement>(KEYS.table), which always returns the context-bound table element from React's element provider. This makes the hook immune to mid-transaction path shifts.Before
After
Changes
packages/table/src/react/useTableElement.ts:useTableColSizesnow reads the table element fromuseElement(KEYS.table)instead of re-reading from the editor via a stale path. Removed unuseduseElementSelectorandPathApiimports..changeset/fix-table-grid-enter-crash.md: Patch changeset for@platejs/table.Verification
compileTableElementreceives heading →tableRow.children.forEachthrows on undefined.