1

I have a method(function) in react native and I want to call that method from android native service class.If it is possible, please help me to solve.

Thanks in advance.

1 Answer 1

3

You can send an event from android native code to javascript.

private void sendEvent(ReactContext reactContext,
                       String eventName,
                       @Nullable WritableMap params) {
  reactContext
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(eventName, params);
}
WritableMap params = Arguments.createMap();
sendEvent(reactContext, "SomeEventName", params);

then in your react-native:

import { DeviceEventEmitter } from 'react-native';


componentWillMount() {
  DeviceEventEmitter.addListener('SomeEventName', function(e: Event) {
    // handle event.
  });
}

  componentWillUnmount () {
    DeviceEventEmitter.removeListener('SomeEventName', this.onModalVisible)
  }

you can see it here on the react-native site : https://facebook.github.io/react-native/docs/native-modules-android

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.