Where do I add <script> tags to React components?

Viewed 480

I recently purchased a bootstrap theme, and I am trying to apply it to my React project. There are certain aspects in the project that are loaded with scripts at the bottom of the page right before the end of the body tag, and I am unsure of where to put them in my react component. So given the example:

<body>
//some elements here

 <script type="text/javascript" src="assets/js/jquery.min.js"></script>
 <script type="text/javascript" src="assets/js/popper.min.js"></script>
 <script type="text/javascript" src="assets/js/bootstrap.js"></script>

</body>

Where would I place this code in a React component? I tried right before the end of my div tag but it did not work.

1 Answers

Either you add it in the index.html at public folder, assuming that you are using create-react-app

Or you can add it programmatically, for instance:


function FreshChat(props) {
    const body = useRef();

    useEffect(() => {
        const fc_JS = document.createElement('script');

        if (!window.fcSettings) {
            fc_JS.type = 'text/javascript';
            fc_JS.src = 'https://my-source-of-js.com/js/yourjs.js';
            if (body.current) {
                body.current.appendChild(fc_JS);
            }
        }

        return () => {
            body.current = null;
        };
    }, [body]);

    return <div ref={body} className="print:hidden" />;
}

Related