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

fix: return value of useEditorPlugin changes when rerender #4084

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

regmsif
Copy link

@regmsif regmsif commented Feb 18, 2025

Before, if there are two tables in one document, selecting multiple table cells will cause page crash, like #4077. Now it will work normally.

The problem is that useEditorPlugin doesn't return a stable value. In useSelectedCells, the setOption returned by useEditorPlugin will change in every re-render. While the useSelectedCells called by the first table sets selectedCells to a non-null value, the useSelectedCells called by the second table will set selectedCells back to null, and it repeats infinitely.

  // for the second table, `selected = false`, so it will set `selectedCells` to null when `setOption` changes
  React.useEffect(() => {
    if (!selected || readOnly) {
      setOption('selectedCells', null);
      setOption('selectedTables', null);
    }
  }, [selected, editor, readOnly, setOption]);

Removing setOption from dependency array also fixes this problem, but I'm not sure whether there are similar problems in other functions, so I make the return value of useEditorPlugin memorized.

Checklist

  • yarn typecheck
  • yarn lint:fix
  • yarn test
  • yarn brl
  • yarn changeset
  • ui changelog

Copy link

codesandbox bot commented Feb 18, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

vercel bot commented Feb 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plate ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 18, 2025 1:10pm

Copy link

changeset-bot bot commented Feb 18, 2025

🦋 Changeset detected

Latest commit: 6b8e2f8

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

This PR includes changesets to release 3 packages
Name Type
@udecode/plate-core Patch
@udecode/plate-utils Patch
@udecode/plate 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

Copy link
Member

@zbeyens zbeyens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for the PR!

@zbeyens zbeyens merged commit c36ff01 into udecode:main Feb 18, 2025
6 checks passed
@regmsif regmsif deleted the fix/use-editor-plugin-rerender branch February 18, 2025 13:21
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

Successfully merging this pull request may close these issues.

2 participants