0

Usually I import a lib to a JavaScript project this way

<script src="../lib/someLib.js"></script>
var theLib = new someLibConstructor("test");

I am very new with react and I am not sure how to achieve this. I importe the lib

import someLib from '../lib/someLib.js';

but how can I call the constructor now?

EDIT:

This is how the library looks like

class someLibConstructor {
    constructor(t) {

    }
export default someLibConstructor

Now I am doing this:

import someLibConstructor from '../lib/someLib.js';

var s = new someLibConstructor("test");

I get an error Type error :

... is not a constructor"

meh
  • 33
  • 1
  • 5
  • It should be the same as before. – Joel Aug 15 '18 at 14:24
  • Possible duplicate of [How do I use/include third party libraries in react?](https://stackoverflow.com/questions/45658200/how-do-i-use-include-third-party-libraries-in-react) – Impurity Aug 15 '18 at 14:25
  • Also mind that if you're using a dependency in node modules, you shouldn't be importing it directly, but instead importing it from the name of the node package. Tell me if that is the case, and I'll edit my answer to explain how that's done. – Filipe Aug 15 '18 at 17:33

2 Answers2

0

Deppends where this constructor is exported. If on someLib.js, there is a line like export default someLibConstructor, you can import it with import someLibConstructor from '../lib/someLib' (The .js is not necessary). But, if it is exported not as default, you have to import { someLibConstructor } from '../lib/someLib'

Filipe
  • 866
  • 5
  • 16
  • There is a line `export default someLibConstructor` but when I do import someLibConstructor from '../lib/someLib' , someLibConstructor is undefined – meh Aug 15 '18 at 14:37
  • The import worked but when I do `let s = someLibConstructor("test"); ` I get an error `(...) is not a function` – meh Aug 15 '18 at 14:43
  • yeah, its a constructor, not a function. You should instead do ```let s = new someLibConstructor("test");``` – Filipe Aug 15 '18 at 17:28
0

You would use the name you provided it on import.

var theLib = new someLib("test");
Dmitriy
  • 1,211
  • 1
  • 11
  • 28
  • What does the library code that you are exporting from look like? Also do you have any errors in the console? – Dmitriy Aug 15 '18 at 14:33
  • So in your case the constructor is not returning anything, try making your constructor look like this class someLibConstructor { constructor(t) { this.t = t } export default someLibConstructor – Dmitriy Aug 15 '18 at 14:59