HeligPfleigh / react-native-thermal-receipt-printer

A RN library for thermal printer
175 stars 103 forks source link

Net Printer init of null #175

Open GalaxyTea1 opened 7 months ago

GalaxyTea1 commented 7 months ago

I am using Expo and trying to connect to a POS machine on the same LAN network as my device (I have successfully done so using a PC). I don't understand what's happening here, I get err [TypeError: Cannot read property 'init' of null. I would appreciate some help! My code:

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

const PrinterComponent = () => {
  const [printers, setPrinters] = useState([]);
  const [currentPrinter, setCurrentPrinter] = useState();

  useEffect(() => {
    NetPrinter.init()
      .then(() => {
        setPrinters({ host: "192.168.1.12", port: 9100 });
      })
      .catch((error) => console.warn(error));
  }, []);

  const connectPrinter = (host, port) => {
    NetPrinter.connectPrinter(host, port).then(
      (printer) => setCurrentPrinter(printer),
      (error) => console.warn(error),
    );
  };

  const printTextTest = () => {
    if (currentPrinter) {
      NetPrinter.printText("<C>sample text</C>\n");
    }
  };

  return (
    <View>
      {printers.map((printer, index) => (
        <TouchableOpacity
          key={index}
          onPress={() => connectPrinter(printer.host, printer.port)}
        >
          <Text>{`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`}</Text>
        </TouchableOpacity>
      ))}
      <TouchableOpacity onPress={printTextTest}>
        <Text>Print Text</Text>
      </TouchableOpacity>
    </View>
  );
};

export default PrinterComponent;
thecodecafe commented 6 months ago

Hey @GalaxyTea1 if this is an issue on iOS ensure pod-install ran successfully.

Also if you're using Expo then you might need to dig further on how to install libraries that introduce native code correctly.

GalaxyTea1 commented 6 months ago

Hey @GalaxyTea1 if this is an issue on iOS ensure pod-install ran successfully.

Also if you're using Expo then you might need to dig further on how to install libraries that introduce native code correctly.

I use android with Expo run dev, maybe lib does not support it.

thecodecafe commented 6 months ago

Most likely the case @GalaxyTea1, to test the hypothesis, try booting a fresh react native project with react-native cli and see if the lib works as expected. If it does then we will have to jump ship.

petermoreira18 commented 6 months ago

ould appreciate s

Hi @GalaxyTea1 , I had the same issue and I'm also using expo run dev. Currently it's working but there is something that i cannot explain yet.

Notes:

GalaxyTea1 commented 5 months ago

It works with Expo dev, I haven't tried it with the product version. But I was advised to use react native instead of expo, so I changed @petermoreira18