Skip to content
Open
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-18 - Zustand store destructuring causes excessive re-renders
**Learning:** Using `const { a, b } = useStore()` with Zustand subscribes the component to the entire store, causing it to re-render whenever *any* part of the store changes. This is exceptionally problematic in a canvas editor where state (like x/y coordinates of an element during drag) updates continuously.
**Action:** Always use selector functions `const a = useStore(state => state.a)` when accessing Zustand state to ensure the component only re-renders when the specific state it relies on changes.
10 changes: 9 additions & 1 deletion frontend/src/components/CanvasArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ const ElementWrapper: React.FC<ElementWrapperProps> = ({ element, isSelected, on
};

export const CanvasArea = () => {
const { pages, activePageId, selectedElementId, selectElement, updateElement } = useEditorStore();
// ⚡ Bolt Performance Optimization:
// Using selector functions instead of destructuring to prevent unnecessary
// re-renders when other unrelated parts of the Zustand store change.
// This is especially critical in CanvasArea to avoid whole-canvas re-renders during dragging.
const pages = useEditorStore((state) => state.pages);
const activePageId = useEditorStore((state) => state.activePageId);
const selectedElementId = useEditorStore((state) => state.selectedElementId);
const selectElement = useEditorStore((state) => state.selectElement);
const updateElement = useEditorStore((state) => state.updateElement);

const activePage = pages.find(p => p.id === activePageId) || pages[0];
const [scale] = useState(1);
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useEditorStore } from '../store/useEditorStore';

export const Sidebar = () => {
const { pages, activePageId, addPage, setActivePage, selectedElementId } = useEditorStore();
// ⚡ Bolt Performance Optimization:
// Using selector functions instead of destructuring to prevent unnecessary
// re-renders when other unrelated parts of the Zustand store change.
const pages = useEditorStore((state) => state.pages);
const activePageId = useEditorStore((state) => state.activePageId);
const addPage = useEditorStore((state) => state.addPage);
const setActivePage = useEditorStore((state) => state.setActivePage);
const selectedElementId = useEditorStore((state) => state.selectedElementId);

return (
<div className="w-64 bg-white border-r flex flex-col h-full shadow-sm">
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useEditorStore } from '../store/useEditorStore';
import type { TextElement } from '../types';

export const Toolbar = () => {
const { addElement, activePageId } = useEditorStore();
// ⚡ Bolt Performance Optimization:
// Using selector functions instead of destructuring to prevent unnecessary
// re-renders when other unrelated parts of the Zustand store change.
const addElement = useEditorStore((state) => state.addElement);
const activePageId = useEditorStore((state) => state.activePageId);

const handleAddText = () => {
if (!activePageId) return;
Expand Down