0

I'm new to React and I'm trying to include a library javascript file to my project. I add the script in the index.html.

...
<body>
...
...
<script src="../src/assets/js/scripts-init/demo.js"></script>
</body>
</html>

When I open it in Firefox, I can see the warning and error in the console log. Warning:

The script from “http://localhost:3000/src/assets/js/scripts-init/demo.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.

Error:

Uncaught SyntaxError: expected expression, got '<'  demo.js

What's the correct way to add .js script in React project?

Thanks.

Including the demo.js

// Demo Theme Options

$(function () {

    $('.btn-open-options').click(function () {
        $('.ui-theme-settings').toggleClass('settings-open');
    });

    $('.close-sidebar-btn').click(function () {

        var classToSwitch = $(this).attr('data-class');
        var containerElement = '.app-container';
        $(containerElement).toggleClass(classToSwitch);

        var closeBtn = $(this);

        if (closeBtn.hasClass('is-active')) {
            closeBtn.removeClass('is-active');
        } else {
            closeBtn.addClass('is-active');
        }
    });

    $('.switch-container-class').on('click', function () {

        var classToSwitch = $(this).attr('data-class');
        var containerElement = '.app-container';
        $(containerElement).toggleClass(classToSwitch);

        $(this).parent().find('.switch-container-class').removeClass('active');
        $(this).addClass('active');

    });

    $('.switch-theme-class').on('click', function () {

        var classToSwitch = $(this).attr('data-class');
        var containerElement = '.app-container';

        if (classToSwitch == 'app-theme-white') {
            $(containerElement).removeClass('app-theme-gray');
            $(containerElement).addClass(classToSwitch);
        }

        if (classToSwitch == 'app-theme-gray') {
            $(containerElement).removeClass('app-theme-white');
            $(containerElement).addClass(classToSwitch);
        }

        if (classToSwitch == 'body-tabs-line') {
            $(containerElement).removeClass('body-tabs-shadow');
            $(containerElement).addClass(classToSwitch);
        }

        if (classToSwitch == 'body-tabs-shadow') {
            $(containerElement).removeClass('body-tabs-line');
            $(containerElement).addClass(classToSwitch);
        }

        $(this).parent().find('.switch-theme-class').removeClass('active');
        $(this).addClass('active');

    });

    $('.switch-header-cs-class').on('click', function () {
        var classToSwitch = $(this).attr('data-class');
        var containerElement = '.app-header';

        $('.switch-header-cs-class').removeClass('active');
        $(this).addClass('active');

        $(containerElement).attr('class', 'app-header');
        $(containerElement).addClass('header-shadow ' + classToSwitch);

    });

    $('.switch-sidebar-cs-class').on('click', function () {
        var classToSwitch = $(this).attr('data-class');
        var containerElement = '.app-sidebar';

        $('.switch-sidebar-cs-class').removeClass('active');
        $(this).addClass('active');

        $(containerElement).attr('class', 'app-sidebar');
        $(containerElement).addClass('sidebar-shadow ' + classToSwitch);

    });
});
3
  • post the demo.js content Commented Nov 25, 2020 at 19:25
  • I just added it Commented Nov 25, 2020 at 19:28
  • Remove the comment from the top of the file and add type="text/javascript> to your script tag Commented Nov 25, 2020 at 19:37

1 Answer 1

2

If you are using static files, you should add them into the public folder in your react project, for example:

YOUR_PROJECT_FOLDER
|_public
  |_index.html
  |_static
    |_scripts-init
      |_demo.js

And then just refer to it with relative path, like:

<script src="/static/scripts-init/demo.js"></script>

This is because the code gets transpiled by webpack (or the default transpiler) and generates a dev build where the 'src' folder does not exist.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Should I add all the static files to the public folder?
Yes, every static file should be inside the public folder structure or hosted in a cdn

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.