Skip to content

Instantly share code, notes, and snippets.

@aem
Last active April 18, 2017 14:09
Show Gist options
  • Save aem/b0338630eb2352f634edcad952c1018a to your computer and use it in GitHub Desktop.
Save aem/b0338630eb2352f634edcad952c1018a to your computer and use it in GitHub Desktop.
import React from 'react'
import { DeviceEventEmitter, NativeModules } from 'react-native'
const addListener = (event, callback) => {
DeviceEventEmitter.addListener(event, ({key, data}) => {
// `key` is the numeric key assigned by the native dispatcher,
// `data` contains the data that the native call actually wanted to pass
NativeModules.ReactResultRouter.routeResults({
key,
data: callback(data)
})
})
}
export default class NativeEventDispatcher extends React.Component {
componentDidMount() {
addListener('DoSomething', ({prop1, prop2}) => {
// prop1 and prop2 are params passed in from native code
return doSomething(prop1, prop2)
})
}
render() {
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment