0

I have a JavaScript file and want to use it in an React Component. The Script looks following:

function XMLtoJSON() {
    ....
} 
var xml2json = new XMLtoJSON();

And an d.ts File with

interface XMLtoJSON {
    ...
}
declare var xml2json: XMLtoJSON;

I tried to use import './xml2json.js' and the js-code is included in the generated js file, but the part that creates the variable xml2json is not executed.

I recive following error in the browser console 'xml2json' is undefined

If it should be important, I use typescript and .tsx-files.
The Project was created with create-react-app MyProject --scripts-version=react-scripts-ts

1
  • xml2json.js should export something in order to be used as import Commented Apr 12, 2017 at 11:12

1 Answer 1

1

Not too familiar with typescript for react but your exporting file should look something like this:

function XMLtoJSON() {
    ....
} 
var xml2json = new XMLtoJSON();
export xml2json;

and the importing file should have this somewhere in the file:

import { xml2json } from './xml2json';
Sign up to request clarification or add additional context in comments.

2 Comments

adding exprt did nont helped somehow. I now rewritten the code as an typescript class which I exported.
ah I forgot it doesn't need .js at the end.

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.