⚡️(frontend) improve heading store
Reset headings only when the headers are not equal to the previous ones. It will prevent unnecessary rerenders.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
|
|
||||||
import { DocsBlockNoteEditor, HeadingBlock } from '../types';
|
import { DocsBlockNoteEditor, HeadingBlock } from '../types';
|
||||||
@@ -24,7 +25,7 @@ export interface UseHeadingStore {
|
|||||||
resetHeadings: () => void;
|
resetHeadings: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useHeadingStore = create<UseHeadingStore>((set) => ({
|
export const useHeadingStore = create<UseHeadingStore>((set, get) => ({
|
||||||
headings: [],
|
headings: [],
|
||||||
setHeadings: (editor) => {
|
setHeadings: (editor) => {
|
||||||
const headingBlocks = editor?.document
|
const headingBlocks = editor?.document
|
||||||
@@ -36,7 +37,9 @@ export const useHeadingStore = create<UseHeadingStore>((set) => ({
|
|||||||
),
|
),
|
||||||
})) as unknown as HeadingBlock[];
|
})) as unknown as HeadingBlock[];
|
||||||
|
|
||||||
set(() => ({ headings: headingBlocks }));
|
if (!_.isEqual(get().headings, headingBlocks)) {
|
||||||
|
set(() => ({ headings: headingBlocks }));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
resetHeadings: () => set(() => ({ headings: [] })),
|
resetHeadings: () => set(() => ({ headings: [] })),
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user