I’m encountering an issue where this error occurs: Warning: Maximum update depth exceeded.
I am using a color picker to change the CSS of of one of my components dynamically, so every time the color changes, it cause a re-render, which I believe is the reason the error is occurring. Below is an example of what would cause the error:
const card = () => {
const [color, setColor] = useState('#FFF')
return (
<>
<Box sx={{backgroundColor: color}}>
some content
</Box>
<ColorPicker
value={color}
onChange={setColor}
/>
</>
)
}
The specific color picker I am using is this one: react-gradient-color-picker. Any assistance is appreciated.
2
Answers
It’s likely you’ve imported the wrong component. The code you provided should work just right.
It’s also possible that the Box component is the cause of the error message, so perhaps try to pass the style object as React
CSS Properties
, rather than through theSX
Prop:Continuous hovering over the colors to choose the desired color from the color picker leads to many state changes and, correspondingly, triggers the warning: ‘Maximum update depth exceeded.
Use debounce and useCallback hook