Skip to content

Commit

Permalink
refactor: unregister path when component unmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jan 19, 2025
1 parent b781b5a commit e2139f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/core/components/DragDropContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ const DragDropContextClient = ({
[data, setPathData]
);

const unregisterPath = useCallback(
(id: string) => {
setPathData((latestPathData = {}) => {
const newPathData = { ...latestPathData };

delete newPathData[id];

return newPathData;
});
},
[data, setPathData]
);

const initialSelector = useRef<{ zone: string; index: number }>(undefined);

return (
Expand Down Expand Up @@ -578,6 +591,7 @@ const DragDropContextClient = ({
areaId: "root",
depth: 0,
registerPath,
unregisterPath,
pathData,
path: [],
}}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ export const DraggableComponent = ({
},
componentType
);

return () => {
ctx?.unregisterPath?.(id);
};
}, [id, zoneCompound, index, componentType]);

const CustomActionBar = useMemo(
Expand Down
1 change: 1 addition & 0 deletions packages/core/components/DropZone/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type DropZoneContext<UserConfig extends Config = Config> = {
activeZones?: Record<string, boolean>;
pathData?: PathData;
registerPath?: (id: string, selector: ItemSelector, label: string) => void;
unregisterPath?: (id: string) => void;
mode?: "edit" | "render";
depth: number;
registerLocalZone?: (zone: string, active: boolean) => void; // A zone as it pertains to the current area
Expand Down

0 comments on commit e2139f5

Please sign in to comment.