有没有办法在反应原生的Android应用程序中使视图组件(由JS编写)更新?

时间:2021-11-12 18:57:25

I am using react-native to build a android app, and I want to know how can I solve this situation: In Java code, some updating must notify the javascript to refresh the view!

我使用react-native来构建一个Android应用程序,我想知道如何解决这种情况:在Java代码中,一些更新必须通知javascript刷新视图!

1 个解决方案

#1


0  

Yes, this is possible. There is more info on this page:

是的,这是可能的。此页面上有更多信息:

https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript

You need to signal the event back in the JavaScript with an emitter.

您需要使用发射器在JavaScript中发回事件。

I've not actually done this in practice as yet but I will have to soon. Let me know how you get on.

我实际上还没有在实践中做到这一点,但我将很快。让我知道你是怎么办的。

To use their example, in your Java:

要在Java中使用他们的示例:

...
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); 

Then in your JS:

然后在你的JS中:

var { DeviceEventEmitter } = require('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);
},

or, you can directly listen for the event in your JS:

或者,您可以直接在JS中监听事件:

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

#1


0  

Yes, this is possible. There is more info on this page:

是的,这是可能的。此页面上有更多信息:

https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript

You need to signal the event back in the JavaScript with an emitter.

您需要使用发射器在JavaScript中发回事件。

I've not actually done this in practice as yet but I will have to soon. Let me know how you get on.

我实际上还没有在实践中做到这一点,但我将很快。让我知道你是怎么办的。

To use their example, in your Java:

要在Java中使用他们的示例:

...
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); 

Then in your JS:

然后在你的JS中:

var { DeviceEventEmitter } = require('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);
},

or, you can directly listen for the event in your JS:

或者,您可以直接在JS中监听事件:

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