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?

5
  • 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 Commented Dec 13, 2022 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? Commented Dec 13, 2022 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\"}" Commented Dec 14, 2022 at 3: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 Commented Dec 14, 2022 at 8:29
  • Exactly, you could do this manually or find a library in your native language that does it Commented Dec 14, 2022 at 12:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.