-
- Notifications
You must be signed in to change notification settings - Fork 2.7k
Open
Description
Describe the bug
DiagramContext.jsx contained impure state updates where side effects (like updating the undo/redo stack) were occurring directly inside state setter functions. This can lead to unpredictable behavior, double-invocation in React Strict Mode, and state inconsistencies. Additionally, the context value was not memoized, causing unnecessary re-renders for all consuming components. Workspace.jsx also contained mixed concerns, handling both UI layout and complex data persistence logic.
To Reproduce
Steps to reproduce the behavior:
- Open src/context/DiagramContext.jsx.
- Observe addTable, deleteTable, etc., where setUndoStack is called inside
setTablesorsetRelationshipscallbacks. - Observe the
valueobject passed toDiagramContext.Provideris created on every render withoutuseMemo.
Expected behavior
- State updates should be pure. Side effects like updating history (undo/redo) should happen outside of state setters.
- Context values should be memoized to prevent performance degradation.
- Data persistence logic should be separated from UI components for better maintainability.
Additional context
I have implemented the following fixes:
- Refactored DiagramContext.jsx to move side effects out of state updaters and added
useMemofor the context value. - Extracted the diagram load/save/autosave logic from Workspace.jsx into a new custom hook src/hooks/useDiagramIO.js.
- Simplified Workspace.jsx to focus on layout and component composition.
Metadata
Metadata
Assignees
Labels
No labels