How do I solve this problem? #15833
Replies: 3 comments
|
The error means the async watcher resumes after the object it updates has become null (often after the Unity component rerenders/unmounts). Guard the target and invalidate stale async work before assigning: watch(source, async (value, _old, onCleanup) => {
let active = true
onCleanup(() => { active = false })
const result = await loadUnity(value)
if (!active || !unityState.value) return
unityState.value.isUnityLoaded = result
})If |
|
The guard from the comment above fixes the crash; the missing half is cancelling the async work when the component goes away, which stops the stale write from even being attempted: const stopped = ref(false); onBeforeUnmount(() => { watch(source, async () => { await heavyUnityOperation(); if (stopped.value) return; // unmounted mid-await: drop the write Two structural notes that keep this from recurring: |
|
Hi @community! The Fix: Guard the Ref and Clean Up Asynchronous WorkUse import { watch, ref, onWatcherCleanup } from 'vue';
watch(source, async (newVal) => {
let active = true;
onWatcherCleanup(() => { active = false; });
const result = await loadUnityInstance(newVal);
if (!active || !unityWebGLRef.value) return;
unityWebGLRef.value.isUnityLoaded = result;
}); |
Uh oh!
There was an error while loading. Please reload this page.
[vue warn]:Error in callback for watcher **(Promise/async): "TypeError: Cannot set properties of null (setting 'isUnityLoaded')' found in --->

All reactions