Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v9] fix: only copy props if same constructor #3429

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion packages/fiber/src/core/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ const colorMaps = ['map', 'emissiveMap', 'sheenColorMap', 'specularColorMap', 'e

const EVENT_REGEX = /^on(Pointer|Click|DoubleClick|ContextMenu|Wheel)/

type ClassConstructor = { new (): void }

// This function applies a set of changes to the instance
export function applyProps<T = any>(object: Instance<T>['object'], props: Instance<T>['props']): Instance<T>['object'] {
const instance = object.__r3f
Expand All @@ -391,7 +393,11 @@ export function applyProps<T = any>(object: Instance<T>['object'], props: Instan
let { root, key, target } = resolve(object, prop)

// Copy if properties match signatures
if (typeof target?.copy === 'function' && target.copy === (value as any).copy) {
if (
target?.copy &&
(value as ClassConstructor | undefined)?.constructor &&
(target as ClassConstructor).constructor === (value as ClassConstructor).constructor
) {
target.copy(value)
}
// Layers have no copy function, we must therefore copy the mask property
Expand Down
21 changes: 21 additions & 0 deletions packages/fiber/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,27 @@ describe('applyProps', () => {

expect(target.value).toBe('initial')
})

it('should not copy if props are supersets of another', async () => {
const copy = jest.fn()

class Material {
copy = copy
}
class SuperMaterial extends Material {
copy = copy
}

const one = new Material()
const two = new SuperMaterial()

const target = { material: one }
applyProps(target, { material: two })

expect(one.copy).not.toHaveBeenCalled()
expect(two.copy).not.toHaveBeenCalled()
expect(target.material).toBe(two)
})
})

describe('updateCamera', () => {
Expand Down
Loading