0

As the external .js file url belongs to the customer, so I can't share that, I need to include the external js file, and call the function from that external js file. I have seen some solution like:

const script = document.createElement('script');
script.src = url;   //(This is external js url)
script.async = true;
document.body.appendChild(script);

But it is not working for me, or may be I don't know how it works

Assume this is the external sample code:

Sample code to call locally, if we manually download the file and import

  let conn = demo("url", false)
  conn.login("username", "myPassword")

How to do same above call after including in the reactjs

 export default function demo (end, sec) {  
    const login = (user, password) => {
            console.log(" sending login message")

    }
   
    const logout = async (user) => {
                    console.log(" sending logout message")
    }


    /*
     * Expose these methods to the client
     */
    return { login, logout }
}
1

2 Answers 2

1

You will need to append your snippet in one lifecycle method like componentDidMount so React can mount it (or load) whenever you component has been mounted. Once is done, you could call the custom method.

If you use class component:

class MyComponent extends Component {

  componentDidMount() {
    const script = document.createElement('script');
    script.src = url;   //(This is external js url)
    script.async = true;
    document.body.appendChild(script);
  }

  render() { 
    return null;
  }
}

Or if you use functional component with hooks

const MyComponent = () => {
  useEffect(() => {
    const script = document.createElement('script');
    script.src = url;   //(This is external js url)
    script.async = true;
    document.body.appendChild(script);
  }, [])
}
Sign up to request clarification or add additional context in comments.

1 Comment

After loading ur external script it should run automatically. If your methods are exposed in the proper context, it should work. Unfortunately, I believe your methods are exposed on in the React context, so you will have to expose them to the window object. Before the return, you could do something like window.login = (...arg) => login(...arg);
0

To add the external js, there are two option

  1. Create an custom script and add the js file asynchronously.
  2. Use third party node module

Below are Script-

const addExternalJs = () => {
   const script = document.createElement("script");
  script.src = "/<path1>/<path2>/yourJSFile.js";
  script.async = true;
  script.onload = () => this.scriptLoaded();

  document.body.appendChild(script);
}
  1. Functional Based Component

    useEffect(() => { addExternalJs(); }, []);

  2. Class Based Component

    componentDidMount() { addExternalJs(); }

2 Comments

thanks, but I still didn't get how call the methods from that external js file
@u_pendra when javascript files will gets loaded, you will be able to find their all function.

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.