Skip to content

Commit

Permalink
Wondermarin#20 fixed a lot of unnecesary renders caused performance i…
Browse files Browse the repository at this point in the history
…ssue
  • Loading branch information
Profesor08 committed Sep 9, 2021
1 parent dbf8474 commit 49600dd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/Saturation.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from "react";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { SaturationProps } from "../interfaces/Saturation.interface";
import { getColorCoordinates } from "../utils/coordinates.util";
import { toColor } from "../utils/toColor.util";
Expand All @@ -11,9 +11,18 @@ export const Saturation = ({ width, height, color, onChange }: SaturationProps):
return { x, y };
}, [color, width, height]);

const updateColor = (x: number, y: number): void => {
onChange(toColor("hsv", { ...color.hsv, s: (x / width) * 100, v: 100 - (y / height) * 100 }));
};
const colorRef = useRef(color);

const updateColor = useCallback(
(x: number, y: number): void => {
onChange(toColor("hsv", { ...colorRef.current.hsv, s: (x / width) * 100, v: 100 - (y / height) * 100 }));
},
[onChange, width, height]
);

useEffect(() => {
colorRef.current = color;
}, [color]);

return (
<Interactive
Expand Down

0 comments on commit 49600dd

Please sign in to comment.