From d441488ab24aabd74188c7f6cf4a61bdb66763c6 Mon Sep 17 00:00:00 2001 From: Cody Bennett <23324155+CodyJasonBennett@users.noreply.github.com> Date: Mon, 26 Jun 2023 03:16:34 -0500 Subject: [PATCH] fix: safely diff instances --- packages/fiber/src/core/renderer.ts | 4 +++- packages/fiber/src/core/utils.ts | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/fiber/src/core/renderer.ts b/packages/fiber/src/core/renderer.ts index 626ad147eb..c83e84e066 100644 --- a/packages/fiber/src/core/renderer.ts +++ b/packages/fiber/src/core/renderer.ts @@ -353,8 +353,10 @@ function createRenderer(_roots: Map, _getEventPriority?: return Boolean(localState.handlers) }, prepareUpdate(instance, _type, oldProps, newProps) { + const localState = instance?.__r3f ?? {} + // Create diff-sets - if (instance.__r3f.primitive && newProps.object && newProps.object !== instance) { + if (localState.primitive && newProps.object && newProps.object !== instance) { return [true] } else { // This is a data object, let's extract critical information about it diff --git a/packages/fiber/src/core/utils.ts b/packages/fiber/src/core/utils.ts index 584ec5ba09..63d04b1aff 100644 --- a/packages/fiber/src/core/utils.ts +++ b/packages/fiber/src/core/utils.ts @@ -177,19 +177,18 @@ export function dispose void; type?: string; [key // Each object in the scene carries a small LocalState descriptor export function prepare(object: T, state?: Partial) { const instance = object as unknown as Instance - if (state?.primitive || !instance.__r3f) { - instance.__r3f = { - type: '', - root: null as unknown as UseBoundStore, - previousAttach: null, - memoizedProps: {}, - eventCount: 0, - handlers: {}, - objects: [], - parent: null, - ...state, - } + instance.__r3f = { + type: '', + root: null as unknown as UseBoundStore, + previousAttach: null, + memoizedProps: {}, + eventCount: 0, + handlers: {}, + objects: [], + parent: null, + ...state, } + return object }