3

In an html page you can include a javascript file by <script src="path"></script> How do you get variables from an external path like this in react, if the path is, for example: <script src="https://unpkg.com/[email protected]/dist/flux-sdk-helpers.min.js"></script>

Thanks for all your help

2
  • 1
    Please can you select an answer to help others that come across this question Commented Aug 9, 2017 at 13:06
  • A good answer is here: stackoverflow.com/a/34425083/1406849 Commented Oct 19, 2017 at 21:08

3 Answers 3

2

Scripts loaded in that fashion normally install themselves as global variables and/or properties of some other global library.

According to the docs, flux-sdk-helpers installs itself as a global variable FluxHelpers when you install it this way.

This means your React code can access it as either FluxHelpers or window.FluxHelpers.

Sign up to request clarification or add additional context in comments.

2 Comments

So, just to wrap my head around it, I include the file in the head in index.html and then do I have to 'import FluxHelpers from '...'; or anything? It gives me errors otherwise
if you include it as a script tag, then you do not need to import it. Just use the variables. However, you may have eslint or compiler settings that will generate an error on unknown variables and since it doesn't know about your script tag, it will throw an error at compile time. Using window.FluxHelpers will "fool" the compiler into working. If it is an eslint error, you can add the variables to the list of globals in eslintrc. Really for this library, its better to use npm install and then import it. The FluxHelper documentation shows how to do that.
1

you can link it in your index.html file just be careful about what you call things when you are doing it as to not re use names in different places

Comments

-1

if you are using react, then you probably should also learn about webpack.

if you will use webpack, then you can import { method} from "package" most scripts you got from NPM (node package manager) very easily.

5 Comments

Importing in this way has nothing to do with external files.
i import all external libriaries this way.
Maybe I'm doing something wrong, but importing as import * from '<URL>' or import '<URL>' did not work for me. can you please show an example that works?
in case of external url yes, i think this can't be done. I meant external files gotten as npm packages.
If you make that clear in the answer I can remove the -1

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.