Skip to content

Commit

Permalink
fix react batching workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola-Bijelic committed Oct 23, 2023
1 parent a776920 commit c0ea6a6
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions packages/slate-react/src/plugin/with-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,13 @@ export const withReact = <T extends BaseEditor>(
}

e.onChange = options => {
// 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
const maybeBatchUpdates =
REACT_MAJOR_VERSION < 18
? ReactDOM.unstable_batchedUpdates
: (callback: () => void) => callback()

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

if (onContextChange) {
onContextChange(options)
}
const onContextChange = EDITOR_TO_ON_CHANGE.get(e)

onChange(options)
})
if (onContextChange) {
onContextChange(options)
}

onChange(options)
}

return e
Expand Down

0 comments on commit c0ea6a6

Please sign in to comment.