Skip to content

Commit

Permalink
refactor: memoize DropZone context for minor perf boost
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jan 1, 2025
1 parent 6be76bc commit df9da03
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions packages/core/components/DropZone/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ReactNode, createContext, useCallback, useState } from "react";
import {
ReactNode,
createContext,
useCallback,
useMemo,
useState,
} from "react";
import { Config, Data } from "../../types";
import { ItemSelector } from "../../lib/get-item";

Expand Down Expand Up @@ -100,21 +106,25 @@ export const DropZoneProvider = ({
[setActiveZones, dispatch]
);

const memoValue = useMemo(
() =>
({
hoveringComponent,
setHoveringComponent,
registerZoneArea,
areasWithZones,
registerZone,
unregisterZone,
activeZones,
...value,
} as DropZoneContext),
[value, hoveringComponent, areasWithZones, activeZones]
);

return (
<>
{value && (
<dropZoneContext.Provider
value={{
hoveringComponent,
setHoveringComponent,
registerZoneArea,
areasWithZones,
registerZone,
unregisterZone,
activeZones,
...value,
}}
>
{memoValue && (
<dropZoneContext.Provider value={memoValue}>
{children}
</dropZoneContext.Provider>
)}
Expand Down

0 comments on commit df9da03

Please sign in to comment.