react-load-script icon indicating copy to clipboard operation
react-load-script copied to clipboard

How to detect if the script is already loaded?

Open daveyx opened this issue 7 years ago • 6 comments

Hi, i use react-load-script to load a script in a component. If this component is instantiated a second time, i wanna detect if the script was loaded before.

Does react-load-script support this functionality?

daveyx avatar Jun 18 '17 06:06 daveyx

https://github.com/blueberryapps/react-load-script/pull/5

...solves that problem.

andresin87 avatar Jun 20 '17 15:06 andresin87

means i don't have to care about it, as of

      if (this.constructor.loadedScripts[url]) {
        onLoad();
        return;
      }

onLoad() will also be called in my second instantiation after the script was successfully loaded, right?

daveyx avatar Jun 21 '17 02:06 daveyx

If the props are changed (typeOf [prop] !== 'function'), It will call onLoad()again @daveyx. That's right.

andresin87 avatar Jun 26 '17 13:06 andresin87

thx @andresin87 any plan for next release in npm?

daveyx avatar Jun 27 '17 00:06 daveyx

Hi @daveyx and @andresin87, we'll make a release as soon as that PR is merged.

jakubkottnauer avatar Jul 07 '17 20:07 jakubkottnauer

@daveyx The current version already calls onLoad() every time the component has been mounted.

So to detect if the script was loaded before, the best would be to track inside your app state if onLoad has been called.

Or you can do:

const isLoaded = Script.loadedScripts[url]

matejlauko avatar Aug 03 '17 12:08 matejlauko