mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add C++ Turbo Module Event Emitter example (#44810)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44810 Adds an example how to use the `EventEmitter` on a (C++) Turbo Module ## Changelog: [General] [Added] - Add C++ Turbo Module Event Emitter example Reviewed By: javache Differential Revision: D57473949 fbshipit-source-id: 1a8d17fb83af4220ef12379e0102b5b2e233ed45
This commit is contained in:
committed by
Facebook GitHub Bot
parent
872d5d3c5e
commit
6a3a305628
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
|
||||
import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter';
|
||||
|
||||
import NativeCxxModuleExample, {
|
||||
EnumInt,
|
||||
@@ -72,6 +73,7 @@ type ErrorExamples =
|
||||
|
||||
class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
|
||||
static contextType: React$Context<RootTag> = RootTagContext;
|
||||
eventSubscriptions: EventSubscription[] = [];
|
||||
|
||||
state: State = {
|
||||
testResults: {},
|
||||
@@ -262,6 +264,34 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
|
||||
'Cannot load this example because TurboModule is not configured.',
|
||||
);
|
||||
}
|
||||
if (NativeCxxModuleExample) {
|
||||
this.eventSubscriptions.push(
|
||||
NativeCxxModuleExample.onPress.addListener(value =>
|
||||
console.log('onPress: ()'),
|
||||
),
|
||||
);
|
||||
this.eventSubscriptions.push(
|
||||
NativeCxxModuleExample.onClick.addListener(value =>
|
||||
console.log(`onClick: (${value})`),
|
||||
),
|
||||
);
|
||||
this.eventSubscriptions.push(
|
||||
NativeCxxModuleExample.onChange.addListener(value =>
|
||||
console.log(`onChange: (${JSON.stringify(value)})`),
|
||||
),
|
||||
);
|
||||
this.eventSubscriptions.push(
|
||||
NativeCxxModuleExample.onSubmit.addListener(value =>
|
||||
console.log(`onSubmit: (${JSON.stringify(value)})`),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
for (const subscription of this.eventSubscriptions) {
|
||||
subscription.remove();
|
||||
}
|
||||
}
|
||||
|
||||
render(): React.Node {
|
||||
|
||||
Reference in New Issue
Block a user