Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net Printer init of null #175

Open
GalaxyTea1 opened this issue Apr 5, 2024 · 3 comments
Open

Net Printer init of null #175

GalaxyTea1 opened this issue Apr 5, 2024 · 3 comments
Labels
question Further information is requested

Comments

@GalaxyTea1
Copy link

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;
@GalaxyTea1 GalaxyTea1 added the question Further information is requested label Apr 5, 2024
@thecodecafe
Copy link

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
Copy link
Author

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants