2

I have a object employeeList which contains objects employee and then each employee object has a person object and address object in it and person and address both contain NSString properties and well as a some function e.g. calculateAge().

I want to pass this employeeList object from the Native side to the react native side but it come up as null, it is because it RN bridge will only send privimite data types.

https://facebook.github.io/react-native/docs/native-modules-ios#argument-types

What is the best way to convert employeeList to a JSON object or a nested map so that it can be viewed in the RN side?

Yahoo
  • 4,093
  • 17
  • 59
  • 85
  • Have a look all answers here: https://stackoverflow.com/questions/10548429/convert-an-ios-objective-c-object-to-a-json-string Might help you. Basicaly use a library to do it. – GeneCode Dec 14 '18 at 00:08
  • I think you need to convert your person and address objects to dictionaries, and resolve them back to the JS side. – Andrew Dec 14 '18 at 17:10
  • @Andrew - Thanks for the comments, Is there any quick way to do it? ( Besisdes using a library, I see RCTConvert does the opposite thing) – Yahoo Dec 14 '18 at 18:46

1 Answers1

1

You can not send through the bridge objects with nested functions as properties of the same object. But you are able to send native functions/methods declared in a self built module in Objective-C or Swift.

I would recommend you to export a function calculateAge() in the native module. This function receives as parameter the JSON object employee a returns the desired age of this employee using callbacks or emitting events although I would use a construction with Promises for that. Also this function could be called when you want on the JS side. However write this kind of function on a native module would make no sense because you can perform the same logic on the JS side.

Export a function in a native module would be useful if the logic needs extra data that only runs on the native side.

Finally make sure that you export an object (NSDictionary) with string keys and values of any type from this list. So you need to convert the object employeeList to a JSON Object and delete nested functions from it in order to avoid compatibility problems.

Helmer Barcos
  • 1,898
  • 12
  • 18