React native The mechanism of publishing events and subscribing events to make native and JavaScript communicate, and individuals feel similar to sending messages in OC.
Here's how to use Rctdeviceeventemitter.
First, in the OC code
1, #import "RCTEventDispatcher.h"
2, @implementation @synthesize bridge = _bridge;
3. Call the following function where the event needs to be published:
_bridge.eventDispatcher sendDeviceEventWithName: (NSString *) eventName body: (id) bodyData
// eventName: the name of the event bodyData: the data to be passed
Second, in the JS code
1. var RCTDeviceEventEmitter = require (‘RCTDeviceEventEmitter’); `
2. Subscribe to events where needed. For example, to subscribe after the component is mounted:
componentDidMount: function () {
this.subscription = RCTDeviceEventEmitter.addListener ('orientationDidChange', (dimensions) => {
console.log (dimensions);
})
},
3. Remember to remove the subscription
componentWillUnMount: function () {
this.subscription.remove ();
},