Skip to content

Commit b5a49fb

Browse files
authored
Merge pull request #6 from smartlabsAT/feature/optimize-duplicate-check-and-useeffect-deps
Optimize duplicate breakpoint detection and fix useEffect dependencies
2 parents 03fdaf8 + 7e73e99 commit b5a49fb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/BreakpointContext.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,26 @@ export const BreakpointProvider: React.FC<BreakpointProviderProps> = ({
8282

8383
/** Check for duplicate breakpoint values */
8484
useEffect(() => {
85-
const duplicates = sortedBreakpoints.filter(
86-
([, value], index) => sortedBreakpoints.findIndex(([, v]) => v === value) !== index
87-
);
85+
const seenValues = new Set<number>();
86+
const duplicates: Array<[Breakpoint, number]> = [];
87+
88+
for (const entry of sortedBreakpoints) {
89+
const [, value] = entry;
90+
if (seenValues.has(value)) {
91+
duplicates.push(entry);
92+
} else {
93+
seenValues.add(value);
94+
}
95+
}
96+
8897
if (duplicates.length > 0) {
8998
if (shouldLog) {
9099
console.error(
91100
'❌ BreakpointProvider: Duplicate breakpoint values detected. This may lead to unexpected behavior.'
92101
);
93102
}
94103
}
95-
}, [sortedBreakpoints]);
104+
}, [sortedBreakpoints, shouldLog]);
96105

97106
// Determine the current breakpoint based on the measured width. 📏
98107
const currentBreakpoint = useMemo(() => {

0 commit comments

Comments
 (0)