Closed ryanmeasel closed 7 years ago
This is actually expected behavior. Native Modules are called across an asynchronous bridge, so there is no way to synchronously return a value to the original caller; if you did so, your react application would be blocked until a response came back.
Instead, if you need to return a result, you can do one of two things: 1) Have your native module method return a promise. 2) Have your native module method accept a callback that is triggered with some value
When a method is called with a callback, a unique identifier is passed to the native side. To trigger it, you can instruct the React Native Context to call that callback with a specific set of arguments.
I highly recommend that you look at how the Native Modules supporting React VR are implemented for more examples: https://github.com/facebook/react-vr/tree/master/ReactVR/js/Modules
This is an example of a Promise-based API (the $ prefix means it returns a Promise on the React side): https://github.com/facebook/react-vr/blob/master/ReactVR/js/Modules/LinkingManager.js#L49 Because it has a Promise API, the final two arguments of the method are assumed to be callbacks to either resolve or reject that promise.
Ah, that makes sense. Thanks for your help, Andrew!
Description
Bug - The return value of a native module function is undefined when accessed in a React component.
Expected behavior
Actual behavior
Reproduction
client.js:
index.vr.js:
Additional Information