Skip to content

Commit 906a455

Browse files
authored
refactor: optimize context value creation and useCallback in hooks (#1820)
1 parent 9b6f462 commit 906a455

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

packages/react/src/editor/BlockNoteView.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,26 @@ function BlockNoteViewComponent<
152152

153153
// We set defaultUIProps and editorProps on a different context, the BlockNoteViewContext.
154154
// This BlockNoteViewContext is used to render the editor and the default UI.
155-
const defaultUIProps = {
155+
const blockNoteViewContextValue = useMemo(() => {
156+
return {
157+
editorProps: {
158+
autoFocus,
159+
contentEditableProps,
160+
},
161+
defaultUIProps: {
162+
formattingToolbar,
163+
linkToolbar,
164+
slashMenu,
165+
emojiPicker,
166+
sideMenu,
167+
filePanel,
168+
tableHandles,
169+
comments,
170+
},
171+
};
172+
}, [
173+
autoFocus,
174+
contentEditableProps,
156175
formattingToolbar,
157176
linkToolbar,
158177
slashMenu,
@@ -161,21 +180,11 @@ function BlockNoteViewComponent<
161180
filePanel,
162181
tableHandles,
163182
comments,
164-
};
165-
166-
const editorProps = {
167-
autoFocus,
168-
contentEditableProps,
169-
};
183+
]);
170184

171185
return (
172186
<BlockNoteContext.Provider value={blockNoteContext}>
173-
<BlockNoteViewContext.Provider
174-
value={{
175-
editorProps,
176-
defaultUIProps,
177-
}}
178-
>
187+
<BlockNoteViewContext.Provider value={blockNoteViewContextValue}>
179188
<ElementRenderer ref={setElementRenderer} />
180189
<BlockNoteViewContainer
181190
className={className}

packages/react/src/hooks/useActiveStyles.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BlockNoteEditor, StyleSchema } from "@blocknote/core";
22

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

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

26-
// Updates state on editor content change.
27-
useEditorChange(() => {
26+
const updateStyles = useCallback(() => {
2827
setStyles(e.getActiveStyles());
29-
}, e);
28+
}, [e]);
29+
30+
// Updates state on editor content change.
31+
useEditorChange(updateStyles, e);
3032

3133
// Updates state on selection change.
32-
useEditorSelectionChange(() => {
33-
setStyles(e.getActiveStyles());
34-
}, e);
34+
useEditorSelectionChange(updateStyles, e);
3535

3636
return styles;
3737
}

packages/react/src/hooks/useSelectedBlocks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
InlineContentSchema,
66
StyleSchema,
77
} from "@blocknote/core";
8-
import { useState } from "react";
8+
import { useCallback, useState } from "react";
99
import { useBlockNoteContext } from "../editor/BlockNoteContext.js";
1010
import { useEditorContentOrSelectionChange } from "./useEditorContentOrSelectionChange.js";
1111

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

34-
useEditorContentOrSelectionChange(
34+
const updateSelectedBlocks = useCallback(
3535
() =>
3636
setSelectedBlocks(
3737
e.getSelection()?.blocks || [e.getTextCursorPosition().block],
3838
),
39-
e,
39+
[e],
4040
);
4141

42+
useEditorContentOrSelectionChange(updateSelectedBlocks, e);
43+
4244
return selectedBlocks;
4345
}

0 commit comments

Comments
 (0)