0

I am calling a JavaScript SDK and invoking it's function in following fashion in my HTML code and it is working fine as expected.

Here is the code

<!DOCTYPE html>

<head>
    <title>My Test App</title>

    <script src="scripts/abc.js"></script>
    <script src="scripts/xyz.js" onload="initXYZ('Param1')"></script>
</head>


</html>

Now, I want to call this same Javascript SDK from a react web page. I want to call the scripts and invoke the initXYZ('Param1') function.

So far, I am able to load the SDK, but I am not sure how to call the function as i did above. Here is the code I wrote in react app.

import React, {useEffect, useRef} from "react";
import "./App.css"

const App = () => {
  const instance = useRef(null);

  useEffect(() => {
    const settingsTag = document.createElement("script");
    settingsTag.src = "scripts/abc.js";
    instance.current.appendChild(settingsTag);

    const websdkTag = document.createElement("script");
    websdkTag.src = "scripts/xyz.js";
    instance.current.appendChild(websdkTag);

    

  }, []);
  return (
    <>
    <h1>My React app</h1>
    <div ref={instance} >
    </>
  );
  
};
export default App;

Can you please help me to understand how to invoke the function in above code. Also is there a better way to what I did here?

9
  • I think you have to import as type="module". Does this work? Commented Jun 29, 2021 at 12:26
  • Define the initXYZ() function on the window object. So window.initXYZ = () => console.log('init'); Commented Jun 29, 2021 at 12:26
  • settingsTag.type= "module"; Commented Jun 29, 2021 at 12:26
  • @celsiuss where should i call this line of code? Inside HTML? Commented Jun 29, 2021 at 12:30
  • @Rohit that entirely depends on your app and what you want to do in that function. Preferably not inside the html. Preferably in some init script you have that will be run before you load the external js libs. Commented Jun 29, 2021 at 13:19

2 Answers 2

2
+50

This is what I do with my own SDK.

Simply creates as many classes as you have sections in your desired SDK. Embed all those sub SDKs into an object SDK that you reference from your app. See how it might look like:

Sdk.js

// From here, pretend the following is the content of your sdk.js

class SdkAbc {
  static shared // The singleton
  constructor() {

    // If already created, returned the singleton
    if (SdkAbc.shared) return SdkAbc.shared

    // Actually create the instance
    SdkAbc.shared = this

    // You might want to write your init code right below
    
  }

  version() { 
     return 1.3
  }
    
}

class SdkXyz {
   // Same as above
}

// You SDK singleton can host as many SDK sections as you need
const Sdk = {
   abc: new SdkAbc(),
   xyz: new SdkXyz(),
}

export default Sdk

App.js

    import React, {useEffect, useRef} from "react";
    import "./App.css"

import sdk from './sdk/sdk' // provided sdk.js is your sdk file located in /sdk folder



    const App = () => {
    
      return (
        <>
        <h1>My React app</h1>
        <div>Sdk Abc version: {Sdk.abc.version()} >
        </>
      );
      
    };
    export default App;
Sign up to request clarification or add additional context in comments.

Comments

2

you can use react-helmet to add any js file to page

Comments

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.