2015-12-16 57 views

回答

0

是的,這是可能的。有此頁面上的詳細信息:

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

你需要有發射信號的事件早在JavaScript的。

我還沒有在實踐中真正做到這一點,但我將盡快。讓我知道你是怎麼辦的。

要使用他們爲榜樣,在你的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); 

然後在您的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); 
}, 

或者,你可以直接聆聽到您的JS事件:

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