Create-react-app: how to jqeury importing to index.html

Created on 2 Jun 2017  路  6Comments  路  Source: facebook/create-react-app

I am gonna to import jquery to index.html.
So I can use jquery in all js file without import $ from 'jquery'.

But it is not working as my thought.

Is it impossible?

Thanks is advance...

Most helpful comment

As I suggested above, instead of adding it to index.html run:

npm install --save jquery

and then use

import $ from 'jquery';

However, if you insist on adding it to HTML, you need to add const $ = window.$ to the top of the file as described in using global variables.

All 6 comments

Not sure if it's a problem in the source but you made a typo in the package name.

import $ from 'jquery';

should work fine if your jquery version in package.json is higher than 3.x and you ran npm install.

Thanks @gaearon
Sorry wrong spell.
But it is not reason.

This is my case.
added this to index.html
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

added jquery to app.js
class App extends Component {
componentDidMount() {
$('.App-header').addClass('someClass')
}
render() {
return (
~~~
);
}
}

http://prntscr.com/ff489d

Maybe try window.$

As I suggested above, instead of adding it to index.html run:

npm install --save jquery

and then use

import $ from 'jquery';

However, if you insist on adding it to HTML, you need to add const $ = window.$ to the top of the file as described in using global variables.

Thanks again.
Anyway... These method is complex...
I remember, before I used $ without any importing.
Only imported it in index html as common js importing...

@starcraft0429 What happens is because of eslint, you can find the reason from google-is-not-defined-in-react-app-using-create-react-app

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dualcnhq picture dualcnhq  路  3Comments

oltsa picture oltsa  路  3Comments

Aranir picture Aranir  路  3Comments

DaveLindberg picture DaveLindberg  路  3Comments

fson picture fson  路  3Comments