我想用蓝牙热敏打印机在react native中打印收据



你好,我想用蓝牙热敏打印机在react native中打印收据。我有代码,因为现在它的工作,但给一个警告。有了这个代码,我可以打印收据,请帮助摆脱这个警告也。这是我的代码。TypeError:null不是一个对象(求值'RNBLEPrinter.init')


import React, { useState, useEffect } from "react";
import {
USBPrinter,
NetPrinter,
BLEPrinter,
} from "react-native-thermal-receipt-printer";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";

function Print() {
const [printers, setPrinters] = useState([]);
const [currentPrinter, setCurrentPrinter] = useState();

useEffect(() => {
BLEPrinter.init().then(() => {
BLEPrinter.getDeviceList().then(setPrinters);
});
}, []);

(_connectPrinter) => (printer) => {
//connect printer
BLEPrinter.connectPrinter(printer.inner_mac_address).then(
setCurrentPrinter,
(error) => console.warn(error)
);
};

printTextTest = () => {
currentPrinter && USBPrinter.printText("this is new print");
};

printBillTest = () => {
currentPrinter && USBPrinter.printBill("this is for bill testing");
};
return (
<View style={styles.container}>
{printers.map((printer) => (
<TouchableOpacity
key={printer.inner_mac_address}
onPress={() => _connectPrinter(printer)}
>
{`device_name: ${printer.device_name}, inner_mac_address: ${printer.inner_mac_address}`}
</TouchableOpacity>
))}
<TouchableOpacity onPress={printTextTest}>
<Text>Print Text</Text>
</TouchableOpacity>
<TouchableOpacity onPress={printBillTest}>
<Text>Print Bill Text</Text>
</TouchableOpacity>
</View>
);
}
export default Print;

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
});

这看起来可能来自react-native-thermal-receipt-printer库。我会在node模块中查找,通常在index.js文件中,它会进行init调用。

相关内容

  • 没有找到相关文章

最新更新