How to include a frontend script when using create-react-app

548 views Asked by At

I'm trying to add the jquery module select2 to my react project which has been created by create-react-app.

I'm running

npm install --save select2

and can see that the dependency is correctly added to my package.json.

Then I add this to a global javascript-file outside my react-code:

$(function () {
    $('select').select2();
}); 

This does not work as the select2 script is not included.

What am I doing wrong? Should I somehow run the jquery code inside my react code? Should I do something specific to include the select2 library?

This might be a very basic problem, but I'm confused how scripts are actually included when running under the framework of create-react-app.

1

There are 1 answers

0
Nikola Schou On

After som more research I realize that I can do the following:

  1. Run 'npm install --save jquery'. This will add jquery to package.json

  2. Add this import statement to my component: 'import $ from 'jquery';'

Any comments or more correct solutions will still be appreciated.