0

我使用Android Zebra扫描仪执行扫描,我使用DeviceEventEmitter ('datawedge_broadcast_intent') 在第一个屏幕上,我使用DeviceEventEmitter,一切正常,当我更换屏幕时,DeviceEventEmitter无法捕获最新状态,

如果我触发 DeviceEventEmitter 或 onPress,则 dataReceived () 函数将运行,但在 DeviceEventEmitter 中,dataReceived () 函数不会从 count 中捕获最新数据。这是我的代码如下:

import React, { useEffect, useState } from 'react';
import {
  Button, DeviceEventEmitter,
  StyleSheet,
  Text, View
} from 'react-native';
// import DataWedgeIntents from 'react-native-datawedge-intents';

const Unkitting = () => {
  const [count, setCount] = useState(0);

  const dataReceived = intent => {
    console.log(intent)
    setCount(count + 1)
  };

  useEffect(() => {
    const scan = DeviceEventEmitter.addListener(
      'datawedge_broadcast_intent', intent => { dataReceived(intent); },
    );
    return () => scan.remove();
  }, []);

  return (
    <View >
      <Text>{count}</Text>
      <Button
        title="add count"
        onPress={() => dataReceived("Button Press")}>
      </Button>
    </View>
  );
};

export default Unkitting;
4

0 回答 0