Skip to content

fix(table): read table element from context to prevent stale-path crash on Enter (#5064) - #5072

Closed
Samarth1306w wants to merge 1 commit into
udecode:nextfrom
Samarth1306w:fix/table-grid-enter-crash-5064
Closed

fix(table): read table element from context to prevent stale-path crash on Enter (#5064)#5072
Samarth1306w wants to merge 1 commit into
udecode:nextfrom
Samarth1306w:fix/table-grid-enter-crash-5064

Conversation

@Samarth1306w

@Samarth1306w Samarth1306w commented Aug 5, 2026

Copy link
Copy Markdown
  • Auto release

Summary

Fixes #5064 — Homepage editor throws from table grid code when pressing Enter on next.

Root Cause

useTableColSizes reads the table path via useElementSelector and 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 into compileTableGridcompileTableElement, which expects table rows but receives heading children, causing:

TypeError: Cannot read properties of undefined (reading 'forEach')
  at packages/table/src/lib/internal/grid.ts:138

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

const tablePath = useElementSelector(([, path]) => path, { name: KEYS.table });
const overriddenColSizes = useEditorSelector(() => {
  const tableNode = editor.read.nodes.get<TTableElement>(tablePath)?.[0];
  // tablePath can be stale → wrong node → crash
  ...
});

After

const tableElement = useElement<TTableElement>(KEYS.table);
const overriddenColSizes = React.useMemo(() => {
  if (!tableElement) return [];
  // Always the correct, context-bound table node
  ...
}, [tableElement, colSizeOverrides, disableOverrides, editor, transformColSizes]);

Changes

  • packages/table/src/react/useTableElement.ts: useTableColSizes now reads the table element from useElement(KEYS.table) instead of re-reading from the editor via a stale path. Removed unused useElementSelector and PathApi imports.
  • .changeset/fix-table-grid-enter-crash.md: Patch changeset for @platejs/table.

Verification

  • Traced the crash path: Enter → block insert → path shift → stale path resolves to heading → compileTableElement receives heading → tableRow.children.forEach throws on undefined.
  • The fix eliminates the stale-path dependency entirely by using React context, which is always in sync with the rendered element.

…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
@codesandbox

codesandbox Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@changeset-bot

changeset-bot Bot commented Aug 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6611d53

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@platejs/table Patch

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

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working patch Bugfix & documentation PR plugin:table Tables labels Aug 5, 2026
@zbeyens zbeyens closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working patch Bugfix & documentation PR plugin:table Tables size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants