提问人:greta calamari 提问时间:11/30/2022 最后编辑:Jason Allergreta calamari 更新时间:3/11/2023 访问量:1229
在 React Native 中从本机模块向 JavaScript 发送事件?
Sending Events to JavaScript from Your Native Module in React Native?
问:
我正在尝试从我的模块传递一个事件以本机反应。我在这里按照以下步骤操作,但它没有按预期工作。 我没有收到错误,但它根本不起作用。
这是我在模块内的 Android Studio 中的代码:
package com.coinboxcollector;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
@ReactModule(name = CoinboxCollectorModule.NAME)
public class CoinboxCollectorModule extends ReactContextBaseJavaModule {
public static final String NAME = "CoinboxCollector";
public CoinboxCollectorModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@NonNull
@Override
public String getName() {
return "CoinBoxCollector";
}
private static String port="/dev/tty.usbserial-fa14";
public MyCCT cct;
public CoinObserver coinObserver = new CoinObserver() {
private ReactContext reactContext;
@Override
public void update(MyCCT cct, float coin) {
Log.d("Coin", Float.toString(coin));
try {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onReceivedCoin", coin);
}catch (Exception ex){
}
}
};
@ReactMethod
public void initialize(Promise promise)
{
try{
Log.d("stato", "inizializzazione ");
if(cct == null){
cct = new MyCCT(port, this.getReactApplicationContext());
}
cct.addCoinObserver(coinObserver);
cct.start();
promise.resolve(true);
}catch (Exception err){
promise.resolve(false);
}
}
@ReactMethod
public void stop(Promise promise)
{
try{
cct.stop();
cct = null;
promise.resolve(true);
}catch (Exception err){
promise.resolve(false);
}
}
@ReactMethod
public void addListener(String eventName) {
}
@ReactMethod
public void removeListeners(Integer count) {
}
@ReactMethod
public void multiply(double a, double b, Promise promise) {
promise.resolve(a * b);
}
}
我在react native中的代码:
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useRef, useState} from 'react';
import {
Alert,
DeviceEventEmitter,
NativeEventEmitter,
NativeModules,
Pressable,
StyleSheet,
Text,
View,
} from 'react-native';
const {CoinBoxCollector} = NativeModules;
export default function HomeScreen() {
const [off, setOff] = useState(true);
const navigation = useNavigation();
let coinInserted = 0;
const coinValueStartExperience = 2;
async function onCoinDetected(coin: any) {
Alert.alert('ricevuti soldi', coin);
console.log('ricevuti soldi');
}
const pressInit = async () => {
return await CoinBoxCollector.initialize();
};
const pressStop = async () => {
return await CoinBoxCollector.stop();
};
useEffect(() => {
const eventEmitter = new NativeEventEmitter(NativeModules.CoinBoxCollector);
const eventListener = eventEmitter.addListener(
'onReceivedCoin',
onCoinDetected,
);
return eventListener.remove();
}, []);
return (
<View style={styles.container}>
<Pressable style={styles.btn} onPress={pressInit}>
<Text style={styles.btnText}>Initialize</Text>
</Pressable>
<Pressable style={styles.btn} onPress={pressStop}>
<Text style={styles.btnText}>Stop it</Text>
</Pressable>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignContent: 'center',
},
btn: {
marginTop: 30,
backgroundColor: '#841584',
width: '5%',
height: 30,
marginLeft: '48%',
},
btnText: {
color: 'white',
textAlign: 'center',
},
});
总的来说,我正在尝试做所有这些工作,以从事件中获取硬币(我插入硬币箱中的硬币)并将其置于未来状态。 提前感谢大家的帮助
答:
1赞
Douglas Nassif Roma Junior
3/11/2023
#1
您按照文档操作了“本机模块”的事件。
正确的文档是“本机组件”的事件。
https://reactnative.dev/docs/native-components-android#events
评论