0

I am successfully calling a JavaScript callback in a Swift native module in React Native. I need to pass data from the Native side back to React Native, but when the data is anything other than strings or numbers, the data parameter in the callback is null. I need to pass back an array of objects as a parameter but for testing purposes I have created an instance of a Swift class:

    func displayProviderDialog(_ mvpds: [Any]) {
        
        class City: NSObject {
            var name: String = "TEST STRING"
        }
        
        var city = City();
        
        rNCallbacks.displayProviderDialogCallback([mvpds, city])
    }

The in Typescript on the React Native side:

export function displayProviderDialog(MVPDs: unknown[], city: any) {
    console.log(`${JSON.stringify(MVPDs)} >>>>>>>>>>>>> ${JSON.stringify(city)}}`
    );
}

Both MVPDs and city are null in React Native, but are not null in Swift / Xcode.

How can I pass back arrays and objects from Swift native modules to React Native?

Mr. Robot
  • 1,334
  • 6
  • 27
  • 79
  • 1
    I'm not sure if user-created objects are supported. You could stringify it as JSON on the native side and parse it in JS, slightly annoying but quite reliable – Abe Dec 13 '22 at 20:34
  • Ok thanks - I’ve been trying that and not been able to do it - what I need to pass back is ‘mvpds’ which is ‘[Any]’ - do you know how I can stringify that? – Mr. Robot Dec 13 '22 at 20:41
  • Since `any` can be anything, no, I do not know exactly how to do that. But your `City` would be `"{\"name\":\"TEST STRING\"}"` – Abe Dec 14 '22 at 03:22
  • Thanks @Abe, that example seems like a string literal, I would need some way of stringifying the object at runtime with somekind of method – Mr. Robot Dec 14 '22 at 08:29
  • Exactly, you could do this manually or find a library in your native language that does it – Abe Dec 14 '22 at 12:50

0 Answers0