5

Actually I am new here for react-native application development. And I am trying to develop a small react-native application with database connectivity (AWS Dynamo db). I select a java as a server side language. And I want to know, is there any possible to import java classes in react-native (i.e. accessing java methods from react-native).

Thanks.

1 Answer 1

4

Yes, you can, with a bit of glue code. React Native has a concept of Native Modules that allows you to interface with any Java code.

Once you've created your native module according to the above linked instructions, you can decorate any method on the module with a @ReactMethod annotation to make it available in JavaScript:

public class YourDynamoDBModule extends ReactContextBaseJavaModule {

  // ...
  @ReactMethod
  public void arbitraryMethod(string query, Promise promise) {
      // do something with your third-party library
        Promise.resolve(yourResponse);
  }
  //...
}

You'll need a bit more boilerplate than that (it's all explained in the docs). Once that is in place, you can call it from JavaScript like follows:

import { NativeModules } from 'react-native';

NativeModules
  .YourDynamoDBModule
  .arbitraryMethod('...')
  .then(result => {

  });

It's worth noting that all native module calls are inherently asynchronous, so if you need to pass data back to JavaScript you need to use promises (as above) or callbacks.

Sign up to request clarification or add additional context in comments.

Comments

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.