MCPcopy
hub / github.com/pmndrs/react-three-fiber / applyProps

Function applyProps

packages/fiber/src/core/utils.tsx:401–535  ·  view source on GitHub ↗
(object: Instance<T>['object'], props: Instance<T>['props'])

Source from the content-addressed store, hash-verified

399
400// This function applies a set of changes to the instance
401export function applyProps<T = any>(object: Instance<T>['object'], props: Instance<T>['props']): Instance<T>['object'] {
402 const instance = object.__r3f
403 const rootState = instance && findInitialRoot(instance).getState()
404 const prevHandlers = instance?.eventCount
405
406 for (const prop in props) {
407 let value = props[prop]
408
409 // Don't mutate reserved keys
410 if (RESERVED_PROPS.includes(prop)) continue
411
412 // Deal with pointer events, including removing them if undefined
413 if (instance && EVENT_REGEX.test(prop)) {
414 if (typeof value === 'function') instance.handlers[prop as keyof EventHandlers] = value as any
415 else delete instance.handlers[prop as keyof EventHandlers]
416 instance.eventCount = Object.keys(instance.handlers).length
417 continue
418 }
419
420 // Ignore setting undefined props
421 // https://github.com/pmndrs/react-three-fiber/issues/274
422 if (value === undefined) continue
423
424 let { root, key, target } = resolve(object, prop)
425
426 // Throw an error if we attempted to set a pierced prop to a non-object
427 if (target === undefined && (typeof root !== 'object' || root === null)) {
428 throw Error(`R3F: Cannot set "${prop}". Ensure it is an object before setting "${key}".`)
429 }
430
431 // Layers must be written to the mask property
432 if (target instanceof THREE.Layers && value instanceof THREE.Layers) {
433 target.mask = value.mask
434 }
435 // Set colors if valid color representation for automatic conversion (copy)
436 else if (target instanceof THREE.Color && isColorRepresentation(value)) {
437 target.set(value)
438 }
439 // Copy if properties match signatures and implement math interface (likely read-only)
440 else if (
441 target !== null &&
442 typeof target === 'object' &&
443 typeof target.set === 'function' &&
444 typeof target.copy === 'function' &&
445 (value as ClassConstructor | null)?.constructor &&
446 (target as ClassConstructor).constructor === (value as ClassConstructor).constructor
447 ) {
448 target.copy(value)
449 }
450 // Set array types
451 else if (
452 target !== null &&
453 typeof target === 'object' &&
454 typeof target.set === 'function' &&
455 Array.isArray(value)
456 ) {
457 if (typeof target.fromArray === 'function') target.fromArray(value)
458 else target.set(...value)

Callers 5

configureFunction · 0.90
handleContainerEffectsFunction · 0.90
swapInstancesFunction · 0.90
commitUpdateFunction · 0.90
utils.test.tsFile · 0.90

Calls 7

findInitialRootFunction · 0.85
resolveFunction · 0.85
ErrorFunction · 0.85
isColorRepresentationFunction · 0.85
invalidateInstanceFunction · 0.85
setMethod · 0.80
setScalarMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…