Skip to content

fix: Improve React Re-render Loops #1820

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

Merged
merged 1 commit into from
Jul 8, 2025
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
35 changes: 22 additions & 13 deletions packages/react/src/editor/BlockNoteView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,26 @@ function BlockNoteViewComponent<

// We set defaultUIProps and editorProps on a different context, the BlockNoteViewContext.
// This BlockNoteViewContext is used to render the editor and the default UI.
const defaultUIProps = {
const blockNoteViewContextValue = useMemo(() => {
return {
editorProps: {
autoFocus,
contentEditableProps,
},
defaultUIProps: {
formattingToolbar,
linkToolbar,
slashMenu,
emojiPicker,
sideMenu,
filePanel,
tableHandles,
comments,
},
};
}, [
autoFocus,
contentEditableProps,
formattingToolbar,
linkToolbar,
slashMenu,
Expand All @@ -161,21 +180,11 @@ function BlockNoteViewComponent<
filePanel,
tableHandles,
comments,
};

const editorProps = {
autoFocus,
contentEditableProps,
};
]);

return (
<BlockNoteContext.Provider value={blockNoteContext}>
<BlockNoteViewContext.Provider
value={{
editorProps,
defaultUIProps,
}}
>
<BlockNoteViewContext.Provider value={blockNoteViewContextValue}>
<ElementRenderer ref={setElementRenderer} />
<BlockNoteViewContainer
className={className}
Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/hooks/useActiveStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BlockNoteEditor, StyleSchema } from "@blocknote/core";

import { useState } from "react";
import { useCallback, useState } from "react";
import { useBlockNoteContext } from "../editor/BlockNoteContext.js";
import { useEditorChange } from "./useEditorChange.js";
import { useEditorSelectionChange } from "./useEditorSelectionChange.js";
Expand All @@ -23,15 +23,15 @@ export function useActiveStyles<T extends StyleSchema>(

const [styles, setStyles] = useState(() => e.getActiveStyles());

// Updates state on editor content change.
useEditorChange(() => {
const updateStyles = useCallback(() => {
setStyles(e.getActiveStyles());
}, e);
}, [e]);

// Updates state on editor content change.
useEditorChange(updateStyles, e);

// Updates state on selection change.
useEditorSelectionChange(() => {
setStyles(e.getActiveStyles());
}, e);
useEditorSelectionChange(updateStyles, e);

return styles;
}
8 changes: 5 additions & 3 deletions packages/react/src/hooks/useSelectedBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
InlineContentSchema,
StyleSchema,
} from "@blocknote/core";
import { useState } from "react";
import { useCallback, useState } from "react";
import { useBlockNoteContext } from "../editor/BlockNoteContext.js";
import { useEditorContentOrSelectionChange } from "./useEditorContentOrSelectionChange.js";

Expand All @@ -31,13 +31,15 @@ export function useSelectedBlocks<
Block<BSchema, ISchema, SSchema>[]
>(() => e.getSelection()?.blocks || [e.getTextCursorPosition().block]);

useEditorContentOrSelectionChange(
const updateSelectedBlocks = useCallback(
() =>
setSelectedBlocks(
e.getSelection()?.blocks || [e.getTextCursorPosition().block],
),
e,
[e],
);

useEditorContentOrSelectionChange(updateSelectedBlocks, e);

return selectedBlocks;
}
Loading