Skip to content

Commit

Permalink
fix(responsive): infinite loop when computing props
Browse files Browse the repository at this point in the history
  • Loading branch information
abelflopes committed Jul 25, 2024
1 parent 304128a commit fc0b6d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
12 changes: 1 addition & 11 deletions packages/components/responsive/src/hooks/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { eachBreakpoint } from "../utils";
* @returns @see {@link UseResponsiveData}
*/
export const useBreakpoints = (
active: boolean,
active: boolean, // TODO: enable activating for specific breakpoints
target: ResponsiveTarget = "viewport",
): {
breakpointsData: EnabledBreakpointsMapping;
Expand Down Expand Up @@ -97,16 +97,6 @@ export const useBreakpoints = (
};
}, [active, target]);

useEffect(() => {
// eslint-disable-next-line no-console -- temp debug / TODO: remove
console.log("useBreakpoints: breakpointsData", breakpointsData);
}, [breakpointsData]);

useEffect(() => {
// eslint-disable-next-line no-console -- temp debug / TODO: remove
console.log("useBreakpoints: activeBreakpoint", activeBreakpoint);
}, [activeBreakpoint]);

return useMemo(
() => ({
breakpointsData,
Expand Down
15 changes: 5 additions & 10 deletions packages/components/responsive/src/hooks/responsive-props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from "react";
import { useMemo } from "react";
import { type BaseProps, type ResponsiveProps } from "../types";
import { useBreakpoints } from "./breakpoints";
import { eachBreakpoint } from "../utils";
Expand All @@ -14,13 +14,10 @@ export const useResponsiveProps = <T extends BaseProps>({
const { target, ...responsiveProps } = responsive ?? {};
const active = useMemo(() => Boolean(responsive), [responsive]);
const { breakpointsData } = useBreakpoints(active, target);
const [computedProps, setComputedProps] = useState<T>(baseProps);

useEffect(() => {
if (!active) {
setComputedProps(baseProps);
return;
}
return useMemo<T>(() => {
// use default props when not active
if (!active) return baseProps;

let tmpProps: T = baseProps;

Expand All @@ -36,8 +33,6 @@ export const useResponsiveProps = <T extends BaseProps>({
};
});

setComputedProps(tmpProps);
return tmpProps;
}, [breakpointsData, baseProps, active, responsiveProps]);

return computedProps;
};

0 comments on commit fc0b6d6

Please sign in to comment.