Skip to content

Commit

Permalink
fix: use flushSync when moving line editor since we need to read prev…
Browse files Browse the repository at this point in the history
…ious value after setting state (#5508)

* fix: use flushSync when moving line editor since we need to read previous value after setting state

* add comment
  • Loading branch information
ad1992 committed Jul 29, 2022
1 parent 46f5ce5 commit 3d5356c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/App.tsx
@@ -1,4 +1,5 @@
import React, { useContext } from "react";
import { flushSync } from "react-dom";
import { RoughCanvas } from "roughjs/bin/canvas";
import rough from "roughjs/bin/rough";
import clsx from "clsx";
Expand Down Expand Up @@ -2681,7 +2682,12 @@ class App extends React.Component<AppProps, AppState> {
this.state.gridSize,
);
if (editingLinearElement !== this.state.editingLinearElement) {
this.setState({ editingLinearElement });
// Since we are reading from previous state which is not possible with
// automatic batching in React 18 hence using flush sync to synchronously
// update the state. Check https://github.com/excalidraw/excalidraw/pull/5508 for more details.
flushSync(() => {
this.setState({ editingLinearElement });
});
}
if (editingLinearElement.lastUncommittedPoint != null) {
this.maybeSuggestBindingAtCursor(scenePointer);
Expand Down

2 comments on commit 3d5356c

@vercel
Copy link

@vercel vercel bot commented on 3d5356c Jul 29, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

excalidraw-package-example – ./src/packages/excalidraw

excalidraw-package-example-excalidraw.vercel.app
excalidraw-package-example-git-master-excalidraw.vercel.app
excalidraw-package-example.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3d5356c Jul 29, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.