Skip to content

Commit 61b123d

Browse files
jhornemangaearon
authored andcommitted
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
1 parent 8cf45e1 commit 61b123d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/react-scripts/template/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
2020
- [Post-Processing CSS](#post-processing-css)
2121
- [Adding Images and Fonts](#adding-images-and-fonts)
2222
- [Using the `public` Folder](#using-the-public-folder)
23+
- [Using Global Variables Defined Elsewhere](#using-global-variables-defined-elsewhere)
2324
- [Adding Bootstrap](#adding-bootstrap)
2425
- [Adding Flow](#adding-flow)
2526
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
@@ -384,6 +385,22 @@ Keep in mind the downsides of this approach:
384385

385386
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.
386387

388+
Note that if you add a `<script>` that declares global variables, you also need to read the next section on using them.
389+
390+
## Using Global Variables Defined Elsewhere
391+
392+
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.
393+
394+
You can avoid this by reading the global variable explicitly from the `window` object, for example:
395+
396+
```js
397+
const $ = window.$;
398+
```
399+
400+
This makes it obvious you are using a global variable intentionally rather than because of a typo.
401+
402+
Alternatively, you can force the linter to ignore any line by adding `// eslint-disable-line` after it.
403+
387404
## Adding Bootstrap
388405

389406
You don’t 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:

0 commit comments

Comments
 (0)