Skip to content

Commit

Permalink
fix: Infinite useColor re-renders (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Wondermarin <[email protected]>
  • Loading branch information
hug0b and Wondermarin authored Nov 7, 2021
1 parent 6c8fa99 commit 3dcc66b
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/hooks/useColor.hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Color } from "../interfaces/Color.interface";
import { useCallback, useEffect, useState } from "react";
import { useState } from "react";
import { toColor } from "../utils/toColor.util";

/**
Expand All @@ -24,29 +24,18 @@ export function useColor<M extends keyof Color, C extends Color[M]>(
model: M,
initColor: C
): [Color, React.Dispatch<React.SetStateAction<Color>>] {
const getColor = useCallback(() => {
if (model === "hex") {
const color = initColor as Color["hex"];

return toColor("hex", color);
} else if (model === "rgb") {
const color = initColor as Color["rgb"];

return toColor("rgb", color);
} else if (model === "hsv") {
const color = initColor as Color["hsv"];

return toColor("hsv", color);
const [color, setColor] = useState(() => {
switch (model) {
case "hex":
return toColor("hex", initColor as Color["hex"]);
case "rgb":
return toColor("rgb", initColor as Color["rgb"]);
case "hsv":
return toColor("hsv", initColor as Color["hsv"]);
default:
return toColor("hex", "#121212");
}

return toColor("hex", "#000000");
}, [model, initColor]);

const [color, setColor] = useState(getColor);

useEffect(() => {
setColor(getColor);
}, [getColor]);
});

return [color, setColor];
}

0 comments on commit 3dcc66b

Please sign in to comment.