Home > OS >  Import a selfcalling function from an npm module into a react/webpack app
Import a selfcalling function from an npm module into a react/webpack app

Time:11-18

I've build a react app transpiled with webpack and now want to use a package pulled from npm (namely vanilla-cookieconsent). Alas, it's written pretty old school, there is no export or nothing, instead it's just a self calling function that adds a method to the window object. And for the life of me I can't seem to get it into my react/webpack combo.

So, what is the correct way to load and use such a function in a transpiled react app?

CodePudding user response:

Doesn't simply something like

import '~vanilla-cookieconsent/cookieconsent.css';

...

useEffect(() => {
  import('~vanilla-cookieconsent/cookieconsent.js').then(() => {
    // Loading the script should have injected this to window
    window.cookieconsent.run(...);
  });
}, []);

do the trick?

  • Related