Skip to content
Advertisement

React Constants inside a Function

I need help to export the constants. I am getting different errors when i try to search for this on google or other related topics at stackoverflow.

This is my Printer.jsx

import React, { useRef, useState } from "react";


// export individual features (can export var, let,
// const, function, class)
export let ePosDev = new window.epson.ePOSDevice();
export const ePosDevice = useRef();
export const printer = useRef();

export function connectFunction() { 
  ePosDevice.current = ePosDev;
  ePosDev.connect("192.168.1.254", 8080, (data) => {
  if (data === "OK") {
    ePosDev.createDevice(
      "local_printer",
      ePosDev.DEVICE_TYPE_PRINTER,
      { crypto: true, buffer: false },
      (devobj, retcode) => {
        if (retcode === "OK") {
          printer.current = devobj;
        } else {
          throw retcode;
        }
      }
    );
  } else {
    throw data;
  }
}); };

  

I need to add the const connect to the App.js so that if the App is starting the connection is also starting. The second is that i need to add the const print to ReactB.js-file so if content of ReactB.js-page is loading the print-request should be send.

Thanks for your help! Stuck at this since 5 hours and dont know how to deal with this problems.

Advertisement

Answer

It seems your main issue stems around how to export constants. I recommend checking out MDN for more info: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

Below is an excerpt on named exports that is relevant to your scenario.

// export features declared earlier
export { myFunction, myVariable };

// export individual features (can export var, let,
// const, function, class)
export let myVariable = Math.sqrt(2);
export function myFunction() { ... };

So for your example, it would just be a matter of adding declaring the const with export const connect = value; or adding export { connect }; after it is declared.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement