Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/hooks/useRive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,32 @@ export default function useRive(
if (!canvasElem || !riveParams) {
return;
}
let isLoaded = rive != null;
let r: Rive | null;
if (rive == null) {
const { useOffscreenRenderer } = options;
const r = new Rive({
r = new Rive({
useOffscreenRenderer,
...riveParams,
canvas: canvasElem,
});
r.on(EventType.Load, () => {
isLoaded = true;
// Check if the component/canvas is mounted before setting state to avoid setState
// on an unmounted component in some rare cases
if (canvasElem) {
setRive(r);
} else {
// If unmounted, cleanup the rive object immediately
r.cleanup();
r!.cleanup();
}
});
}
return () => {
if(!isLoaded) {
r?.cleanup();
}
}
}, [canvasElem, isParamsLoaded, rive]);
/**
* Ref callback called when the container element mounts
Expand Down
Loading