Add emitter so I can now send events to the RN core

This commit is contained in:
Artur Gurgul 2025-08-02 15:19:12 +02:00
parent 4e4a92c2a3
commit 7177b9e927
7 changed files with 111 additions and 14 deletions

35
App.tsx
View file

@ -1,19 +1,30 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import { NewAppScreen } from '@react-native/new-app-screen'
import { useEffect, useState } from 'react'
import { StatusBar, Text, StyleSheet, useColorScheme, View } from 'react-native'
import { NativeEventEmitter, NativeModules } from 'react-native'
const { Emitter } = NativeModules;
import { NewAppScreen } from '@react-native/new-app-screen';
import { StatusBar, StyleSheet, useColorScheme, View } from 'react-native';
export default function App() {
const isDarkMode = useColorScheme() === 'dark'
const [message, setMessage] = useState(null)
useEffect(() => {
const emitter = new NativeEventEmitter(Emitter);
const subscription = emitter.addListener('onMessage', (event) => {
if (event?.message) {
setMessage(event.message)
}
})
function App() {
const isDarkMode = useColorScheme() === 'dark';
return () => {
subscription.remove()
};
}, [])
return (
<View style={styles.container}>
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
<Text>{message ?? 'Waiting for message...'}</Text>
<NewAppScreen templateFileName="App.tsx" />
</View>
);
@ -23,6 +34,4 @@ const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default App;
})