I have a data visualization component that has "width" as one of the props. A state keeps track of the zoom level in the chart, and it is updated when handling mouse events.
The zoom needs to adjust when the width changes. Currently, I'm doing this inside a useEffect hook:
function MyComponent({width}) {
const [zoom, setZoom] = useState(...)
useEffect(() => {
setZoom(adjustZoom(zoom, width))
}, [width])
const handleMouseEvent = (event) => {
setZoom(calculateNewZoom(event))
}
...
}
But this makes the component render twice: once for the width update, and once for the zoom update. Since the first render flashes on screen, it is not ideal.
Ideally, the component would render only once, reflecting both the changes in width and zoom at the same time. How to achieve this with hooks? Also, is there a name for this concept? Thanks in advance.