1

I'm doing a react project using create-react-app and my current goal is to load an external js file (that is hosted in iis) and use their data.

I'm getting this file throught a script in index.html like:

<script type="text/javascript" src="http://localhost:5000/GetJsFile"></script>

Example of my js file:

var data = {
   vars: {
      id: ****,
      user ******,
      name: *******,
      date: *******,
   }
   //...
}

My question is: how can I use/access the data from js file inside a component of react js?

13
  • your example does not contain a json but javascript. Either export / import your js Object or call a json by an online API. Loading json files from your local file system will not work in a browser context but in node. You cannot simply add a script tag and make the src a json file... Commented Dec 11, 2019 at 14:52
  • 1
    The example you presented is not json file. It is a javascript. It defines global variable. You can use window.data to access it. Commented Dec 11, 2019 at 14:52
  • @messerbill sorry, it is js file, I updated the question thx Commented Dec 11, 2019 at 14:56
  • 1
    no - this is definitely not an answer to the question. If you need to use external functions those are usually imported using import or require. Often this is also used to import libraries and stuff out off node_modules Commented Dec 11, 2019 at 15:08
  • 1
    are you talking about data or about logic / functionality? Data will be called using http requests while third-party-functionality will be imported using npm. If your script is not available via npm you can still push it into a git repository and include it using npm Commented Dec 11, 2019 at 15:43

2 Answers 2

1

In your utils.js page

you must change your code to this shape:

utils page

const data = {
   vars: {
      id: ****,
      user ******,
      name: *******,
      date: *******,
   }
   //...
}
export default data;

and after that in other component that you want to use this data write this code:

import data from '../../../../utils';

...
console.log('data', data);
Sign up to request clarification or add additional context in comments.

Comments

0

The problem with loading the JSON's data from the html file directly is that it will not be available for your react code to use.

Since you are loading it from an external source, you need to use something like fetch, axios or superagent to retrieve it.

You can either use async/await or promises.

async function loadJsonFromExternal() {
    const dataFromJSON = await axios.get('http://localhost:5000/GetJson');
    return dataFromJSON;
}

Say you have your component.js file with something like this:

import React, { useEffect } from 'react';
export default function CoolComponent (props) {
  let myName = 'Enigma';
  useEffect(() => {
      loadJsonFromExternal()
      .then((result) => { myName = result.name });
  }, [])
  return (
     <div>My name is: {myName}</div>
  )
}

That would be the approach to do.

1 Comment

the question is not about loading json files anymore but how to import js files....

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.