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

Do not try to batch updates in React >= 18 #5460

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-shirts-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Do not attempt to batch updates manually in React >= 18
4 changes: 2 additions & 2 deletions packages/slate-react/src/components/slate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../hooks/use-slate-selector'
import { EditorContext } from '../hooks/use-slate-static'
import { ReactEditor } from '../plugin/react-editor'
import { IS_REACT_VERSION_17_OR_ABOVE } from '../utils/environment'
import { REACT_MAJOR_VERSION } from '../utils/environment'
import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ export const Slate = (props: {

useIsomorphicLayoutEffect(() => {
const fn = () => setIsFocused(ReactEditor.isFocused(editor))
if (IS_REACT_VERSION_17_OR_ABOVE) {
if (REACT_MAJOR_VERSION >= 17) {
// In React >= 17 onFocus and onBlur listen to the focusin and focusout events during the bubbling phase.
// Therefore in order for <Editable />'s handlers to run first, which is necessary for ReactEditor.isFocused(editor)
// to return the correct value, we have to listen to the focusin and focusout events without useCapture here.
Expand Down
15 changes: 11 additions & 4 deletions packages/slate-react/src/plugin/with-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
NODE_TO_KEY,
} from '../utils/weak-maps'
import { ReactEditor } from './react-editor'
import { REACT_MAJOR_VERSION } from '../utils/environment'

/**
* `withReact` adds React and DOM specific behaviors to the editor.
Expand Down Expand Up @@ -324,11 +325,17 @@ export const withReact = <T extends BaseEditor>(
}

e.onChange = options => {
// COMPAT: React doesn't batch `setState` hook calls, which means that the
// children and selection can get out of sync for one render pass. So we
// have to use this unstable API to ensure it batches them. (2019/12/03)
// COMPAT: React < 18 doesn't batch `setState` hook calls, which means
// that the children and selection can get out of sync for one render
// pass. So we have to use this unstable API to ensure it batches them.
// (2019/12/03)
// https://github.com/facebook/react/issues/14259#issuecomment-439702367
ReactDOM.unstable_batchedUpdates(() => {
const maybeBatchUpdates =
REACT_MAJOR_VERSION < 18
? ReactDOM.unstable_batchedUpdates
: (callback: () => void) => callback()

maybeBatchUpdates(() => {
const onContextChange = EDITOR_TO_ON_CHANGE.get(e)

if (onContextChange) {
Expand Down
3 changes: 1 addition & 2 deletions packages/slate-react/src/utils/environment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'

export const IS_REACT_VERSION_17_OR_ABOVE =
parseInt(React.version.split('.')[0], 10) >= 17
export const REACT_MAJOR_VERSION = parseInt(React.version.split('.')[0], 10)

export const IS_IOS =
typeof navigator !== 'undefined' &&
Expand Down
Loading