Added info on using global variables. (#976)

* Added info on using global variables.

See issue #972.

* Fixed typo.

* Moved new section.

* Small tweaks

* Syntax highlighting
This commit is contained in:
Jurie Horneman
2016-11-20 20:29:45 +01:00
committed by Dan Abramov
parent 8cf45e1fd5
commit 61b123df57

View File

@@ -20,6 +20,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
- [Post-Processing CSS](#post-processing-css)
- [Adding Images and Fonts](#adding-images-and-fonts)
- [Using the `public` Folder](#using-the-public-folder)
- [Using Global Variables Defined Elsewhere](#using-global-variables-defined-elsewhere)
- [Adding Bootstrap](#adding-bootstrap)
- [Adding Flow](#adding-flow)
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
@@ -384,6 +385,22 @@ Keep in mind the downsides of this approach:
However, it can be handy for referencing assets like [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest) from HTML, or including small scripts like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
Note that if you add a `<script>` that declares global variables, you also need to read the next section on using them.
## Using Global Variables Defined Elsewhere
When you include a script in the HTML file that defines global variables and try to use one of these variables in the code, the linter will complain because it cannot see the definition of the variable.
You can avoid this by reading the global variable explicitly from the `window` object, for example:
```js
const $ = window.$;
```
This makes it obvious you are using a global variable intentionally rather than because of a typo.
Alternatively, you can force the linter to ignore any line by adding `// eslint-disable-line` after it.
## Adding Bootstrap
You dont have to use [React Bootstrap](https://react-bootstrap.github.io) together with React but it is a popular library for integrating Bootstrap with React apps. If you need it, you can integrate it with Create React App by following these steps: