8

I would like to include jquery.min.js and add a external JavaScript file and call a JavaScript function inside an existing react JS file.

I did below but it is not working

const VoCaaSComponentbtn = (
        <React.Fragment>
             <div>
            <script type="text/javascript"src="https:xxxx/jquery-3.3.1.min.js"></script> 
            <script type="text/javascript"src="https:xxx/microsurvey" defer="defer"></script>
            <div id="microsurvey" module="true" tab-info="tabname"></div>
            <script type="text/javascript"> voc_getsurvey(); </script>                  
            </div>
        </React.Fragment>
    );

3 Answers 3

23

You can import all the scripts in the main HTML file.

If you want to import the script inside the react component, you can use the following options.

Option 1: Use plain Javascript and React custom hook

Create custom hook [useScript.js]

import { useEffect } from 'react';

const useScript = url => {
  useEffect(() => {
    const script = document.createElement('script');
    script.src = url;
    script.async = true;
    document.body.appendChild(script);
    return () => {
      document.body.removeChild(script);
    };
  }, [url]);
};

export default useScript;

Use

import useScript from 'hooks/useScript';

const MyComponent = props => {
  useScript('https://use.typekit.net/foobar.js');
  // rest of your component
}

Option 2: Use plain Javascript in ComponentDidMount

componentDidMount () {
  const script = document.createElement("script");
  script.src = url;
  script.async = true;
  document.body.appendChild(script);
}

Option 3: Use dangerouslySetInnerHTML

const codeStr = `
  <div>
     <script type="text/javascript"src="https:xxxx/jquery-3.3.1.min.js"></script> 
     <script type="text/javascript"src="https:xxx/microsurvey" defer="defer"></script>
     <div id="microsurvey" module="true" tab-info="tabname"></div>
     <script type="text/javascript"> voc_getsurvey(); </script>                  
  </div>
`

<div dangerouslySetInnerHTML={{ __html: codeStr }} />
Sign up to request clarification or add additional context in comments.

Comments

7

You can add External JS library in indeex.html page inside public directory.

public/index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="theme-color" content="#000000" />
  <meta name="description" content="Web site created using create-react-app" />
  
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
  
  <script type="text/javascript"src="https:xxxx/jquery-3.3.1.min.js"></script> 
  <title>React App</title>
</head>

<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
  
</body>

</html>

Comments

4

One another way to add external JavaScript file. If you only want to add for specific page or component.

componentDidMount() {
   var script = document.createElement('script')
   script.src = // path of external javascript file.
   script.class = "external-script"
   document.body.appendChild(script);
}

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.