4

At the moment I have a react-native App which has data stored in it's redux store.

I followed the instructions provided by https://developer.android.com/guide/topics/ui/accessibility/services to create an accessibility service.

Now I have data from a website in the Accessibility Server e.g. the url and want to ask the react-native context to give me data that is stored in it's redux store (or async storage).

It's pretty easy to call native code from a running react-native app (js), but how would you implement the other way around?

acuntex
  • 147
  • 13

1 Answers1

0

The only supported way to send information the other way is to signal an event on the native side that you've subscribed to on the JavaScript side.

Here's an example from the docs (see Sending Events to JavaScript).

In JavaScript, subscribe to an event:

import { DeviceEventEmitter } from 'react-native';
...

var ScrollResponderMixin = {
  mixins: [Subscribable.Mixin],


  componentWillMount: function() {
    ...
    this.addListenerOn(DeviceEventEmitter,
                       'keyboardWillShow',
                       this.scrollResponderKeyboardWillShow);
    ...
  },
  scrollResponderKeyboardWillShow:function(e: Event) {
    this.keyboardWillOpenTo = e;
    this.props.onKeyboardWillShow && this.props.onKeyboardWillShow(e);
  },

In Java, send the event:

...
private void sendEvent(ReactContext reactContext,
                       String eventName,
                       @Nullable WritableMap params) {
  reactContext
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(eventName, params);
}
...
WritableMap params = Arguments.createMap();
...
sendEvent(reactContext, "keyboardWillShow", params);
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287