Skip to content

Commit 404186c

Browse files
barisozcetinbrianzinn
authored andcommitted
commitUpdate fn
1 parent 9c6885c commit 404186c

File tree

1 file changed

+65
-14
lines changed

1 file changed

+65
-14
lines changed

packages/react-babylonjs/src/ReactBabylonJSHostConfig.ts

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -794,26 +794,77 @@ const ReactBabylonJSHostConfig: HostConfig<
794794
}
795795
},
796796

797+
/* uploadPayload is removed from commitUpdate in new version
798+
https://www.npmjs.com/package/react-reconciler
799+
but type definitions doesn't seem to be updated. */
800+
// @ts-expect-error types are not updated
797801
commitUpdate(
798802
instance: HostCreatedInstance<any>,
799-
updatePayload: UpdatePayload,
800-
type: string /* old, new props and instance handle are extra ignored params */
803+
type: string,
804+
prevProps: Record<string, any>,
805+
nextProps: Record<string, any>
801806
) {
802-
if (updatePayload !== null && !!type) {
803-
if (Array.isArray(updatePayload)) {
804-
updatePayload.forEach((update: PropertyUpdate) => {
805-
if (instance && update) {
806-
applyUpdateToInstance(instance, update)
807-
} else {
808-
// console.warn("skipped applying update to missing instance...", update, type);
809-
}
810-
})
811-
} else {
812-
if (instance) {
813-
applyUpdateToInstance(instance, updatePayload)
807+
const updatePayload: PropertyUpdate[] = []
808+
809+
const allKeys: string[] = []
810+
const keyMap: Record<string, boolean> = {}
811+
812+
for (const key in prevProps) {
813+
if (!keyMap[key]) {
814+
allKeys.push(key)
815+
keyMap[key] = true
816+
}
817+
}
818+
819+
for (const key in nextProps) {
820+
if (!keyMap[key]) {
821+
allKeys.push(key)
822+
keyMap[key] = true
823+
}
824+
}
825+
826+
for (const key of allKeys) {
827+
// Skip internal React properties (starts with '__')
828+
if (key.startsWith('__')) continue
829+
830+
const prevValue = prevProps[key]
831+
const nextValue = nextProps[key]
832+
833+
if (prevValue !== nextValue) {
834+
let changeType: PropChangeType
835+
836+
if (
837+
typeof nextValue === 'number' ||
838+
typeof nextValue === 'string' ||
839+
typeof nextValue === 'boolean' ||
840+
nextValue === null ||
841+
nextValue === undefined
842+
) {
843+
changeType = PropChangeType.Primitive
844+
} else if (Array.isArray(nextValue) && nextValue.every((v) => typeof v === 'number')) {
845+
changeType = PropChangeType.NumericArray
846+
} else {
847+
changeType = PropChangeType.Primitive
814848
}
849+
850+
updatePayload.push({
851+
propertyName: key,
852+
value: nextValue,
853+
changeType,
854+
isSetAccessor: false,
855+
target: undefined,
856+
})
815857
}
816858
}
859+
860+
// Apply updates using your existing logic
861+
if (updatePayload.length > 0 && !!type) {
862+
updatePayload.forEach((update: PropertyUpdate) => {
863+
if (instance && update) {
864+
applyUpdateToInstance(instance, update)
865+
}
866+
})
867+
}
817868
},
818869

819870
removeChildFromContainer: (container: Container, child: HostCreatedInstance<any>) => {

0 commit comments

Comments
 (0)