From b1db10c217aff50807a39d26ef180273b8294303 Mon Sep 17 00:00:00 2001 From: Fabio Cipriani Date: Fri, 20 Jun 2025 23:25:07 +0200 Subject: [PATCH] Fixing the cloneReactChildrenWithProps method to handle those cases where the children property contains a child which is a React.Fragment, for which no prop can be added. toJSON method always returns a [String:Any] dictionary, hence there is no need to have the value type to be "Any?" getLastKnownLocation now returns an optional [String:Any] value, which is compliant with objective-c, in place of the RNMBXLocation type. --- ios/RNMBX/RNMBXLocationModuleV11.swift | 8 ++++---- src/utils/index.ts | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ios/RNMBX/RNMBXLocationModuleV11.swift b/ios/RNMBX/RNMBXLocationModuleV11.swift index 3e66a17368..55a821240e 100644 --- a/ios/RNMBX/RNMBXLocationModuleV11.swift +++ b/ios/RNMBX/RNMBXLocationModuleV11.swift @@ -11,8 +11,8 @@ class RNMBXLocation: NSObject { var timestamp: Date? = nil - func toJSON() -> [String:Any?] { - var coords: [String:Any?] = [:] + func toJSON() -> [String:Any] { + var coords: [String:Any] = [:] if let location = location { coords = coords.merging([ @@ -251,11 +251,11 @@ class RNMBXLocationModule: RCTEventEmitter { throttler.cancel() } - @objc func getLastKnownLocation() -> RNMBXLocation? { + @objc func getLastKnownLocation() -> [String: Any]? { let last = RNMBXLocation() last.heading = _locationProvider.latestHeading last.location = _locationProvider.getLastObservedLocation() - return last + return last.toJSON() } @objc diff --git a/src/utils/index.ts b/src/utils/index.ts index e5547f77f2..d057b30a53 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import React from 'react'; +import React, { ReactNode } from 'react'; import { findNodeHandle, Platform, @@ -70,14 +70,14 @@ export function runNativeMethod( } export function cloneReactChildrenWithProps( - children: Parameters[0], + children: ReactNode, propsToAdd: { [key: string]: string } = {}, ) { if (!children) { return null; } - let foundChildren = null; + let foundChildren: typeof children[] | null = null; if (!Array.isArray(children)) { foundChildren = [children]; @@ -86,9 +86,19 @@ export function cloneReactChildrenWithProps( } const filteredChildren = foundChildren.filter((child) => !!child); // filter out falsy children, since some can be null - return React.Children.map(filteredChildren, (child) => - React.cloneElement(child, propsToAdd), - ); + return React.Children.map(filteredChildren, (child) => { + if (!React.isValidElement(child)) { + return child; + } + + if (child.type === React.Fragment) { + // If the child is a Fragment, return it without adding props + return child; + } + + // Otherwise, clone and add props + return React.cloneElement(child, propsToAdd); + }); } export function resolveImagePath(imageRef: ImageSourcePropType): string {