0

I have some code in React like this:

useEffect(() => {
  TICautocapture(etc..);

That TICautocapture() function is already defined in an external js file, let's say:

http:://some-cdn.example.com/library.js

So how could I use that library methods on my React component?

I need it to be declared inside the Component, otherwise it wont find the name of the function.

by the way, I cant download the file locally because the library is being updated constantly, so I need to import from an external url.

EDIT:

The code of the library is like this:

// http:://some-cdn.example.com/library.js

var TICautocapture = (function(){
  var lib = {...}
  var error_handler;
  var handleError = (error_code, error_callback) => {...}
  function autocapture(container, options){...}

  return autocapture;
})();

if(window.jQuery){
  (function($){
    $.fn.autocapture = function(options){
      TICautocapture(this.attr('id'), options);
    }
  }(jQuery));
}    

And from there I need to use the TICautocapture() method.

6
  • 1
    probably with Webpack externals Commented Mar 25, 2021 at 21:59
  • @LindaPaiste Could you elaborate? I have read that link but I can't find any info about how add external URLs Commented Mar 25, 2021 at 22:11
  • 1
    why not loading the js with axios are else. And then wait for the promise to be resolved. Commented Mar 25, 2021 at 22:14
  • @Sysix how is that? I will update the code of the external library that I need to use Commented Mar 25, 2021 at 22:20
  • Well now I'm not sure if that's the right way if you are just using the package in this component. The webpack externals is a way to make webpack aware of functions that are available in the window/global scope. It's expecting that the scripts are loaded via the <script> tag in your HTML. Commented Mar 25, 2021 at 22:21

2 Answers 2

1

Your file is loading var TICautocapture into the global scope and also setting a property autocapture on window.jQuery.fn.

I am not confident that this is correct, but I would try using Webpack externals by putting the following in your webpack.config.js

module.exports = {
  // ...
  externalsType: 'script',
  externals: {
    autocapture: [
      'http://some-cdn.example.com/library.js',
      'autocapture', // or maybe 'TICautocapture'?
      'TICautocapture',
    ],
  },
};

And then try importing it in your component file:

import {TICautocapture} from "autocapture";
Sign up to request clarification or add additional context in comments.

1 Comment

I'm having errors with the externalsType options: - configuration has an unknown property 'externalsType'. These properties are valid... etc, and also, without that key, I'm getting a unexpected token } whne I try to render the web
-1

If it's just a file you have in the project, you can simply import it to where you need like so:

// if it's the default export
import TICautocapture from "./file.js"; 

or

// if it's NOT the default export and other 
// functions and variables could be imported from that file
import { TICautocapture } from "./file.js"; 

Of course, in the cases I mentioned, you should make sure they're exported in the files they're exported using export or export default.

If the file is being delivered by a CDN, I'm not really sure but I think just by adding it the the root HTML file will make it available everywhere for you.

4 Comments

It's not in the project, like i said, is an external file from an external URL, I can't download it and add to to my project. I need to use it directly from the server.
Plus, look at the code of my question there are not exports, just a varwith a function that I need to use
Once the file is loaded you should be able to access window.jQuery.fn.autocapture or window.TICautocapture since var TICautocapture is in the global scope.
This does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.