Skip to content

Commit b7280ed

Browse files
authored
fix(colorpicker): prevent infinite rerender in React v18 Legacy mode (#1616)
1 parent 08af7bb commit b7280ed

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

packages/colorpickers/.size-snapshot.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"index.cjs.js": {
3-
"bundled": 55191,
4-
"minified": 36328,
5-
"gzipped": 8532
3+
"bundled": 55237,
4+
"minified": 36325,
5+
"gzipped": 8535
66
},
77
"index.esm.js": {
8-
"bundled": 51333,
9-
"minified": 32705,
10-
"gzipped": 8268,
8+
"bundled": 51385,
9+
"minified": 32708,
10+
"gzipped": 8271,
1111
"treeshaked": {
1212
"rollup": {
13-
"code": 26681,
13+
"code": 26692,
1414
"import_statements": 1005
1515
},
1616
"webpack": {
17-
"code": 28822
17+
"code": 28819
1818
}
1919
}
2020
}

packages/colorpickers/src/elements/Colorpicker/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export const Colorpicker = forwardRef<HTMLDivElement, IColorpickerProps>(
5353
retVal.alpha = 100;
5454
}
5555

56+
if (areColorsEqual(retVal, previousComputedColorRef.current)) {
57+
return previousComputedColorRef.current;
58+
}
59+
5660
return retVal;
5761
}, [color, isOpaque, state.color]);
5862

@@ -67,13 +71,11 @@ export const Colorpicker = forwardRef<HTMLDivElement, IColorpickerProps>(
6771
previousStateColorRef.current = state.color;
6872
}, [color, onChange, state.color]);
6973

70-
useEffect(() => {
71-
if (!areColorsEqual(computedColor, previousComputedColorRef.current)) {
72-
dispatch({ type: 'RESET_COLOR', payload: computedColor });
73-
}
74+
if (previousComputedColorRef.current !== computedColor) {
75+
dispatch({ type: 'RESET_COLOR', payload: computedColor });
7476

7577
previousComputedColorRef.current = computedColor;
76-
}, [computedColor]);
78+
}
7779

7880
const handleColorWellChange = useCallback((hsv: IHSVColor) => {
7981
dispatch({

0 commit comments

Comments
 (0)