Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| da4948741e |
+3
-3
@@ -1,4 +1,4 @@
|
|||||||
## 2024-06-25 - Excalidraw Memoization
|
## 2023-10-27 - Excalidraw Component Re-render Performance Penalty
|
||||||
|
|
||||||
**Learning:** The `@excalidraw/excalidraw` package's `Excalidraw` component is exceptionally expensive to re-render. Even though `EditorCanvas` was wrapped in `React.memo`, passing an inline arrow function to `onExcalidrawAPI` in the parent `App` broke memoization, causing severe input lag when typing in completely independent UI elements like the codeblock editor sidebar due to the entire canvas re-rendering.
|
**Learning:** Due to the exceptionally high rendering cost of the `@excalidraw/excalidraw` canvas, inline functions passed as props (like `onChange` and `onExcalidrawAPI`) cause severe performance degradation by defeating `React.memo` and triggering full canvas re-renders when parent state changes.
|
||||||
**Action:** When passing callbacks to heavy third-party components like Excalidraw, always wrap them in `useCallback` hook to preserve their prop stability and maintain `React.memo` benefits, preventing disastrous performance regressions on typing/input.
|
**Action:** Always strictly memoize all callbacks passed to the Excalidraw component using `useCallback` to preserve prop stability and prevent unnecessary, expensive re-renders.
|
||||||
|
|||||||
+4
-4
@@ -359,12 +359,12 @@ function PrivateApp() {
|
|||||||
onClose={closeSidebar}
|
onClose={closeSidebar}
|
||||||
onCreate={handleCreateDrawing}
|
onCreate={handleCreateDrawing}
|
||||||
onSelect={handleSelectDrawing}
|
onSelect={handleSelectDrawing}
|
||||||
onDelete={deleteDrawing}
|
onDelete={(drawingId) => void deleteDrawing(drawingId)}
|
||||||
onTitleChange={setActiveTitle}
|
onTitleChange={setActiveTitle}
|
||||||
onTitleSubmit={submitTitle}
|
onTitleSubmit={() => void submitTitle()}
|
||||||
onPublicationSlugChange={setPublicationSlug}
|
onPublicationSlugChange={setPublicationSlug}
|
||||||
onPublish={publishPublication}
|
onPublish={() => void publishPublication()}
|
||||||
onDisablePublication={disablePublication}
|
onDisablePublication={() => void disablePublication()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { memo } from "react";
|
|
||||||
import { type DrawingMeta, type DrawingPublication } from "../../core/shared";
|
import { type DrawingMeta, type DrawingPublication } from "../../core/shared";
|
||||||
|
|
||||||
type DrawingSidebarProps = {
|
type DrawingSidebarProps = {
|
||||||
@@ -10,14 +9,14 @@ type DrawingSidebarProps = {
|
|||||||
publicationSlug: string;
|
publicationSlug: string;
|
||||||
publicationBusy: boolean;
|
publicationBusy: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onCreate: () => void | Promise<void>;
|
onCreate: () => void;
|
||||||
onSelect: (drawingId: string) => void | Promise<void>;
|
onSelect: (drawingId: string) => void;
|
||||||
onDelete: (drawingId: string) => void | Promise<void>;
|
onDelete: (drawingId: string) => void;
|
||||||
onTitleChange: (title: string) => void;
|
onTitleChange: (title: string) => void;
|
||||||
onTitleSubmit: () => void | Promise<void>;
|
onTitleSubmit: () => void;
|
||||||
onPublicationSlugChange: (slug: string) => void;
|
onPublicationSlugChange: (slug: string) => void;
|
||||||
onPublish: () => void | Promise<void>;
|
onPublish: () => void;
|
||||||
onDisablePublication: () => void | Promise<void>;
|
onDisablePublication: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
function DrawerIcon() {
|
function DrawerIcon() {
|
||||||
@@ -65,7 +64,7 @@ export function DrawingsToggle({ onClick }: { onClick: () => void }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DrawingSidebar = memo(function DrawingSidebar({
|
export function DrawingSidebar({
|
||||||
open,
|
open,
|
||||||
drawings,
|
drawings,
|
||||||
activeId,
|
activeId,
|
||||||
@@ -134,7 +133,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
className="title-input"
|
className="title-input"
|
||||||
value={activeTitle}
|
value={activeTitle}
|
||||||
onChange={(event) => onTitleChange(event.target.value)}
|
onChange={(event) => onTitleChange(event.target.value)}
|
||||||
onBlur={() => void onTitleSubmit()}
|
onBlur={onTitleSubmit}
|
||||||
onKeyDown={(event) => {
|
onKeyDown={(event) => {
|
||||||
if (event.key === "Enter") {
|
if (event.key === "Enter") {
|
||||||
event.currentTarget.blur();
|
event.currentTarget.blur();
|
||||||
@@ -144,7 +143,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="icon-button drawing-delete-button"
|
className="icon-button drawing-delete-button"
|
||||||
onClick={() => void onDelete(drawing.id)}
|
onClick={() => onDelete(drawing.id)}
|
||||||
aria-label={`Delete ${drawing.title}`}
|
aria-label={`Delete ${drawing.title}`}
|
||||||
>
|
>
|
||||||
×
|
×
|
||||||
@@ -172,7 +171,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="secondary-button"
|
className="secondary-button"
|
||||||
onClick={() => void onPublish()}
|
onClick={onPublish}
|
||||||
disabled={publicationBusy}
|
disabled={publicationBusy}
|
||||||
>
|
>
|
||||||
{publication.enabled ? "Update link" : "Publish"}
|
{publication.enabled ? "Update link" : "Publish"}
|
||||||
@@ -180,7 +179,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="secondary-button"
|
className="secondary-button"
|
||||||
onClick={() => void onDisablePublication()}
|
onClick={onDisablePublication}
|
||||||
disabled={!publication.enabled || publicationBusy}
|
disabled={!publication.enabled || publicationBusy}
|
||||||
>
|
>
|
||||||
Unpublish
|
Unpublish
|
||||||
@@ -206,7 +205,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="drawing-link"
|
className="drawing-link"
|
||||||
onClick={() => void onSelect(drawing.id)}
|
onClick={() => onSelect(drawing.id)}
|
||||||
>
|
>
|
||||||
<span className="drawing-title">{drawing.title}</span>
|
<span className="drawing-title">{drawing.title}</span>
|
||||||
</button>
|
</button>
|
||||||
@@ -215,7 +214,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="icon-button"
|
className="icon-button"
|
||||||
onClick={() => void onDelete(drawing.id)}
|
onClick={() => onDelete(drawing.id)}
|
||||||
aria-label={`Delete ${drawing.title}`}
|
aria-label={`Delete ${drawing.title}`}
|
||||||
>
|
>
|
||||||
×
|
×
|
||||||
@@ -227,4 +226,4 @@ export const DrawingSidebar = memo(function DrawingSidebar({
|
|||||||
</aside>
|
</aside>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { memo } from "react";
|
import { memo, useCallback } from "react";
|
||||||
import { Excalidraw } from "@excalidraw/excalidraw";
|
import { Excalidraw } from "@excalidraw/excalidraw";
|
||||||
import type {
|
import type {
|
||||||
AppState,
|
AppState,
|
||||||
@@ -7,6 +7,7 @@ import type {
|
|||||||
ExcalidrawInitialDataState,
|
ExcalidrawInitialDataState,
|
||||||
ExcalidrawProps,
|
ExcalidrawProps,
|
||||||
} from "@excalidraw/excalidraw/types";
|
} from "@excalidraw/excalidraw/types";
|
||||||
|
import type { OrderedExcalidrawElement } from "@excalidraw/excalidraw/element/types";
|
||||||
import "../../../node_modules/@excalidraw/excalidraw/dist/prod/index.css";
|
import "../../../node_modules/@excalidraw/excalidraw/dist/prod/index.css";
|
||||||
import { type ScenePayload } from "../../core/shared";
|
import { type ScenePayload } from "../../core/shared";
|
||||||
import {
|
import {
|
||||||
@@ -56,6 +57,19 @@ export const EditorCanvas = memo(function EditorCanvas({
|
|||||||
onExcalidrawAPI,
|
onExcalidrawAPI,
|
||||||
renderEmbeddable,
|
renderEmbeddable,
|
||||||
}: EditorCanvasProps) {
|
}: EditorCanvasProps) {
|
||||||
|
const handleChange = useCallback(
|
||||||
|
(
|
||||||
|
elements: readonly OrderedExcalidrawElement[],
|
||||||
|
appState: AppState,
|
||||||
|
files: BinaryFiles,
|
||||||
|
) => {
|
||||||
|
onEditorActivity();
|
||||||
|
onSceneChange(sceneFromEditor(elements, appState, files));
|
||||||
|
onSelectionStateChange(getCodeBlockSelectionState(elements, appState));
|
||||||
|
},
|
||||||
|
[onEditorActivity, onSceneChange, onSelectionStateChange],
|
||||||
|
);
|
||||||
|
|
||||||
if (loading || !scene) {
|
if (loading || !scene) {
|
||||||
return <div className="editor-loading">{error ?? "Loading..."}</div>;
|
return <div className="editor-loading">{error ?? "Loading..."}</div>;
|
||||||
}
|
}
|
||||||
@@ -67,13 +81,7 @@ export const EditorCanvas = memo(function EditorCanvas({
|
|||||||
initialData={sceneToInitialData(scene)}
|
initialData={sceneToInitialData(scene)}
|
||||||
excalidrawAPI={onExcalidrawAPI}
|
excalidrawAPI={onExcalidrawAPI}
|
||||||
renderEmbeddable={renderEmbeddable}
|
renderEmbeddable={renderEmbeddable}
|
||||||
onChange={(elements, appState, files) => {
|
onChange={handleChange}
|
||||||
onEditorActivity();
|
|
||||||
onSceneChange(sceneFromEditor(elements, appState, files));
|
|
||||||
onSelectionStateChange(
|
|
||||||
getCodeBlockSelectionState(elements, appState),
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user