Fix: Add check before setting Rive as state variable on Rive instance load

This commit is contained in:
Zach Plata
2022-07-18 15:52:10 -05:00
committed by Zachary Plata
parent d010a55cc0
commit 838ed1abf8

View File

@@ -181,7 +181,13 @@ export default function useRive(
...riveParams,
canvas,
});
r.on(EventType.Load, () => setRive(r));
r.on(EventType.Load, () => {
// Check if the component/canvas is mounted before setting state to avoid setState
// on an unmounted component in some rare cases
if (canvasRef.current) {
setRive(r);
}
});
} else if (canvas === null && canvasRef.current) {
canvasRef.current.height = 0;
canvasRef.current.width = 0;