mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-03-29 00:18:13 +08:00
Bumping all headings one level because we can now
This commit is contained in:
@@ -11,7 +11,7 @@ This project supports [CSS Modules](https://github.com/css-modules/css-modules)
|
||||
|
||||
CSS Modules let you use the same CSS class name in different files without worrying about naming clashes. Learn more about CSS Modules [here](https://css-tricks.com/css-modules-part-1-need/).
|
||||
|
||||
### `Button.module.css`
|
||||
## `Button.module.css`
|
||||
|
||||
```css
|
||||
.error {
|
||||
@@ -19,7 +19,7 @@ CSS Modules let you use the same CSS class name in different files without worry
|
||||
}
|
||||
```
|
||||
|
||||
### `another-stylesheet.css`
|
||||
## `another-stylesheet.css`
|
||||
|
||||
```css
|
||||
.error {
|
||||
@@ -27,7 +27,7 @@ CSS Modules let you use the same CSS class name in different files without worry
|
||||
}
|
||||
```
|
||||
|
||||
### `Button.js`
|
||||
## `Button.js`
|
||||
|
||||
```js
|
||||
import React, { Component } from 'react';
|
||||
@@ -42,7 +42,7 @@ class Button extends Component {
|
||||
}
|
||||
```
|
||||
|
||||
### Result
|
||||
## Result
|
||||
|
||||
No clashes from other `.error` class names
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ title: Adding a Stylesheet
|
||||
|
||||
This project setup uses [Webpack](https://webpack.js.org/) for handling all assets. Webpack offers a custom way of “extending” the concept of `import` beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to **import the CSS from the JavaScript file**:
|
||||
|
||||
### `Button.css`
|
||||
## `Button.css`
|
||||
|
||||
```css
|
||||
.Button {
|
||||
@@ -13,7 +13,7 @@ This project setup uses [Webpack](https://webpack.js.org/) for handling all asse
|
||||
}
|
||||
```
|
||||
|
||||
### `Button.js`
|
||||
## `Button.js`
|
||||
|
||||
```js
|
||||
import React, { Component } from 'react';
|
||||
|
||||
@@ -33,7 +33,7 @@ import { Button } from 'reactstrap';
|
||||
|
||||
Now you are ready to use the imported reactstrap components within your component hierarchy defined in the render method. Here is an example [`App.js`](https://gist.githubusercontent.com/zx6658/d9f128cd57ca69e583ea2b5fea074238/raw/a56701c142d0c622eb6c20a457fbc01d708cb485/App.js) redone using reactstrap.
|
||||
|
||||
### Using a Custom Theme
|
||||
## Using a Custom Theme
|
||||
|
||||
> Note: this feature is available with `react-scripts@2.0.0` and higher.
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ if (process.env.NODE_ENV !== 'production') {
|
||||
|
||||
When you compile the app with `npm run build`, the minification step will strip out this condition, and the resulting bundle will be smaller.
|
||||
|
||||
### Referencing Environment Variables in the HTML
|
||||
## Referencing Environment Variables in the HTML
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.9.0` and higher.
|
||||
|
||||
@@ -78,12 +78,12 @@ Note that the caveats from the above section apply:
|
||||
- Apart from a few built-in variables (`NODE_ENV` and `PUBLIC_URL`), variable names must start with `REACT_APP_` to work.
|
||||
- The environment variables are injected at build time. If you need to inject them at runtime, [follow this approach instead](/docs/generating-dynamic-meta-tags-on-the-server).
|
||||
|
||||
### Adding Temporary Environment Variables In Your Shell
|
||||
## Adding Temporary Environment Variables In Your Shell
|
||||
|
||||
Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the
|
||||
life of the shell session.
|
||||
|
||||
#### Windows (cmd.exe)
|
||||
### Windows (cmd.exe)
|
||||
|
||||
```cmd
|
||||
set "REACT_APP_SECRET_CODE=abcdef" && npm start
|
||||
@@ -91,19 +91,19 @@ set "REACT_APP_SECRET_CODE=abcdef" && npm start
|
||||
|
||||
(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.)
|
||||
|
||||
#### Windows (Powershell)
|
||||
### Windows (Powershell)
|
||||
|
||||
```Powershell
|
||||
($env:REACT_APP_SECRET_CODE = "abcdef") -and (npm start)
|
||||
```
|
||||
|
||||
#### Linux, macOS (Bash)
|
||||
### Linux, macOS (Bash)
|
||||
|
||||
```bash
|
||||
REACT_APP_SECRET_CODE=abcdef npm start
|
||||
```
|
||||
|
||||
### Adding Development Environment Variables In `.env`
|
||||
## Adding Development Environment Variables In `.env`
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.5.0` and higher.
|
||||
|
||||
@@ -117,7 +117,7 @@ REACT_APP_SECRET_CODE=abcdef
|
||||
|
||||
`.env` files **should be** checked into source control (with the exclusion of `.env*.local`).
|
||||
|
||||
#### What other `.env` files can be used?
|
||||
### What other `.env` files can be used?
|
||||
|
||||
> Note: this feature is **available with `react-scripts@1.0.0` and higher**.
|
||||
|
||||
@@ -138,7 +138,7 @@ Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) f
|
||||
> Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need
|
||||
> these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars).
|
||||
|
||||
#### Expanding Environment Variables In `.env`
|
||||
### Expanding Environment Variables In `.env`
|
||||
|
||||
> Note: this feature is available with `react-scripts@1.1.0` and higher.
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ Please be advised that this is also a custom feature of Webpack.
|
||||
**It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).<br>
|
||||
An alternative way of handling static assets is described in the next section.
|
||||
|
||||
### Adding SVGs
|
||||
## Adding SVGs
|
||||
|
||||
> Note: this feature is available with `react-scripts@2.0.0` and higher.
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ sidebar_label: Available scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `npm start`
|
||||
## `npm start`
|
||||
|
||||
Runs the app in the development mode.<br>
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
@@ -14,12 +14,12 @@ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
The page will reload if you make edits.<br>
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
## `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.<br>
|
||||
See the section about [running tests](/docs/running-tests) for more information.
|
||||
|
||||
### `npm run build`
|
||||
## `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.<br>
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
@@ -29,7 +29,7 @@ Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](/docs/deployment) for more information.
|
||||
|
||||
### `npm run eject`
|
||||
## `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ This project setup supports code splitting via [dynamic `import()`](http://2alit
|
||||
|
||||
Here is an example:
|
||||
|
||||
### `moduleA.js`
|
||||
## `moduleA.js`
|
||||
|
||||
```js
|
||||
const moduleA = 'Hello';
|
||||
@@ -17,7 +17,7 @@ const moduleA = 'Hello';
|
||||
export { moduleA };
|
||||
```
|
||||
|
||||
### `App.js`
|
||||
## `App.js`
|
||||
|
||||
```js
|
||||
import React, { Component } from 'react';
|
||||
@@ -49,7 +49,7 @@ This will make `moduleA.js` and all its unique dependencies as a separate chunk
|
||||
|
||||
You can also use it with `async` / `await` syntax if you prefer it.
|
||||
|
||||
### With React Router
|
||||
## With React Router
|
||||
|
||||
If you are using React Router check out [this tutorial](http://serverless-stack.com/chapters/code-splitting-in-create-react-app.html) on how to use code splitting with it. You can find the companion GitHub repository [here](https://github.com/AnomalyInnovations/serverless-stack-demo-client/tree/code-splitting-in-create-react-app).
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ title: Debugging in the Editor
|
||||
|
||||
Visual Studio Code and WebStorm support debugging out of the box with Create React App. This enables you as a developer to write and debug your React code without leaving the editor, and most importantly it enables you to have a continuous development workflow, where context switching is minimal, as you don’t have to switch between tools.
|
||||
|
||||
### Visual Studio Code
|
||||
## Visual Studio Code
|
||||
|
||||
You would need to have the latest version of [VS Code](https://code.visualstudio.com) and VS Code [Chrome Debugger Extension](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome) installed.
|
||||
|
||||
@@ -37,7 +37,7 @@ Start your app by running `npm start`, and start debugging in VS Code by pressin
|
||||
|
||||
Having problems with VS Code Debugging? Please see their [troubleshooting guide](https://github.com/Microsoft/vscode-chrome-debug/blob/master/README.md#troubleshooting).
|
||||
|
||||
### WebStorm
|
||||
## WebStorm
|
||||
|
||||
You would need to have [WebStorm](https://www.jetbrains.com/webstorm/) and [JetBrains IDE Support](https://chrome.google.com/webstore/detail/jetbrains-ide-support/hmhgeddbohgjknpmjagkdomcpobmllji) Chrome extension installed.
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ There are various ways to setup a debugger for your Jest tests. We cover debuggi
|
||||
|
||||
> Note: debugging tests requires Node 8 or higher.
|
||||
|
||||
### Debugging Tests in Chrome
|
||||
## Debugging Tests in Chrome
|
||||
|
||||
Add the following to the `scripts` section in your project's `package.json`
|
||||
|
||||
@@ -36,7 +36,7 @@ After opening that link, the Chrome Developer Tools will be displayed. Select `i
|
||||
|
||||
> Note: the --runInBand cli option makes sure Jest runs test in the same process rather than spawning processes for individual tests. Normally Jest parallelizes test runs across processes but it is hard to debug many processes at the same time.
|
||||
|
||||
### Debugging Tests in Visual Studio Code
|
||||
## Debugging Tests in Visual Studio Code
|
||||
|
||||
Debugging Jest tests is supported out of the box for [Visual Studio Code](https://code.visualstudio.com).
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ sidebar_label: Deployment
|
||||
|
||||
`npm run build` creates a `build` directory with a production build of your app. Set up your favorite HTTP server so that a visitor to your site is served `index.html`, and requests to static paths like `/static/js/main.<hash>.js` are served with the contents of the `/static/js/main.<hash>.js` file.
|
||||
|
||||
### Static Server
|
||||
## Static Server
|
||||
|
||||
For environments using [Node](https://nodejs.org/), the easiest way to handle this would be to install [serve](https://github.com/zeit/serve) and let it handle the rest:
|
||||
|
||||
@@ -23,7 +23,7 @@ Run this command to get a full list of the options available:
|
||||
serve -h
|
||||
```
|
||||
|
||||
### Other Solutions
|
||||
## Other Solutions
|
||||
|
||||
You don’t necessarily need a static server in order to run a Create React App project in production. It works just as fine integrated into an existing dynamic one.
|
||||
|
||||
@@ -49,7 +49,7 @@ The `build` folder with static assets is the only output produced by Create Reac
|
||||
|
||||
However this is not quite enough if you use client-side routing. Read the next section if you want to support URLs like `/todos/42` in your single-page app.
|
||||
|
||||
### Serving Apps with Client-Side Routing
|
||||
## Serving Apps with Client-Side Routing
|
||||
|
||||
If you use routers that use the HTML5 [`pushState` history API](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Adding_and_modifying_history_entries) under the hood (for example, [React Router](https://github.com/ReactTraining/react-router) with `browserHistory`), many static file servers will fail. For example, if you used React Router with a route for `/todos/42`, the development server will respond to `localhost:3000/todos/42` properly, but an Express serving a production build as above will not.
|
||||
|
||||
@@ -94,7 +94,7 @@ When users install your app to the homescreen of their device the default config
|
||||
"start_url": ".",
|
||||
```
|
||||
|
||||
### Building for Relative Paths
|
||||
## Building for Relative Paths
|
||||
|
||||
By default, Create React App produces a build assuming your app is hosted at the server root.<br>
|
||||
To override this, specify the `homepage` in your `package.json`, for example:
|
||||
@@ -115,7 +115,7 @@ For example:
|
||||
<Link to="/today"/> // renders <a href="/calendar/today">
|
||||
```
|
||||
|
||||
#### Serving the Same Build from Different Paths
|
||||
### Serving the Same Build from Different Paths
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.9.0` and higher.
|
||||
|
||||
@@ -127,7 +127,7 @@ If you are not using the HTML5 `pushState` history API or not using client-side
|
||||
|
||||
This will make sure that all the asset paths are relative to `index.html`. You will then be able to move your app from `http://mywebsite.com` to `http://mywebsite.com/relativepath` or even `http://mywebsite.com/relative/path` without having to rebuild it.
|
||||
|
||||
### Customizing Environment Variables for Arbitrary Build Environments
|
||||
## Customizing Environment Variables for Arbitrary Build Environments
|
||||
|
||||
You can create an arbitrary build environment by creating a custom `.env` file and loading it using [env-cmd](https://www.npmjs.com/package/env-cmd).
|
||||
|
||||
@@ -155,13 +155,13 @@ You can specify other environments in the same way.
|
||||
|
||||
Variables in `.env.production` will be used as fallback because `NODE_ENV` will always be set to `production` for a build.
|
||||
|
||||
### [Azure](https://azure.microsoft.com/)
|
||||
## [Azure](https://azure.microsoft.com/)
|
||||
|
||||
See [this](https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321) blog post on how to deploy your React app to Microsoft Azure.
|
||||
|
||||
See [this](https://medium.com/@strid/host-create-react-app-on-azure-986bc40d5bf2#.pycfnafbg) blog post or [this](https://github.com/ulrikaugustsson/azure-appservice-static) repo for a way to use automatic deployment to Azure App Service.
|
||||
|
||||
### [Firebase](https://firebase.google.com/)
|
||||
## [Firebase](https://firebase.google.com/)
|
||||
|
||||
Install the Firebase CLI if you haven’t already by running `npm install -g firebase-tools`. Sign up for a [Firebase account](https://console.firebase.google.com/) and create a new project. Run `firebase login` and login with your previous created Firebase account.
|
||||
|
||||
@@ -234,11 +234,11 @@ Now, after you create a production build with `npm run build`, you can deploy it
|
||||
|
||||
For more information see [Add Firebase to your JavaScript Project](https://firebase.google.com/docs/web/setup).
|
||||
|
||||
### [GitHub Pages](https://pages.github.com/)
|
||||
## [GitHub Pages](https://pages.github.com/)
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.2.0` and higher.
|
||||
|
||||
#### Step 1: Add `homepage` to `package.json`
|
||||
### Step 1: Add `homepage` to `package.json`
|
||||
|
||||
**The step below is important!**<br>
|
||||
**If you skip it, your app will not deploy correctly.**
|
||||
@@ -263,7 +263,7 @@ or for a custom domain page:
|
||||
|
||||
Create React App uses the `homepage` field to determine the root URL in the built HTML file.
|
||||
|
||||
#### Step 2: Install `gh-pages` and add `deploy` to `scripts` in `package.json`
|
||||
### Step 2: Install `gh-pages` and add `deploy` to `scripts` in `package.json`
|
||||
|
||||
Now, whenever you run `npm run build`, you will see a cheat sheet with instructions on how to deploy to GitHub Pages.
|
||||
|
||||
@@ -304,7 +304,7 @@ additional modifications:
|
||||
+ "deploy": "gh-pages -b master -d build",
|
||||
```
|
||||
|
||||
#### Step 3: Deploy the site by running `npm run deploy`
|
||||
### Step 3: Deploy the site by running `npm run deploy`
|
||||
|
||||
Then run:
|
||||
|
||||
@@ -312,13 +312,13 @@ Then run:
|
||||
npm run deploy
|
||||
```
|
||||
|
||||
#### Step 4: Ensure your project’s settings use `gh-pages`
|
||||
### Step 4: Ensure your project’s settings use `gh-pages`
|
||||
|
||||
Finally, make sure **GitHub Pages** option in your GitHub project settings is set to use the `gh-pages` branch:
|
||||
|
||||
<img src="http://i.imgur.com/HUjEr9l.png" width="500" alt="gh-pages branch setting">
|
||||
|
||||
#### Step 5: Optionally, configure the domain
|
||||
### Step 5: Optionally, configure the domain
|
||||
|
||||
You can configure a custom domain with GitHub Pages by adding a `CNAME` file to the `public/` folder.
|
||||
|
||||
@@ -328,16 +328,16 @@ Your CNAME file should look like this:
|
||||
mywebsite.com
|
||||
```
|
||||
|
||||
#### Notes on client-side routing
|
||||
### Notes on client-side routing
|
||||
|
||||
GitHub Pages doesn’t support routers that use the HTML5 `pushState` history API under the hood (for example, React Router using `browserHistory`). This is because when there is a fresh page load for a url like `http://user.github.io/todomvc/todos/42`, where `/todos/42` is a frontend route, the GitHub Pages server returns 404 because it knows nothing of `/todos/42`. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:
|
||||
|
||||
- You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to `hashHistory` for this effect, but the URL will be longer and more verbose (for example, `http://user.github.io/todomvc/#/todos/42?_k=yknaj`). [Read more](https://reacttraining.com/react-router/web/api/Router) about different history implementations in React Router.
|
||||
- Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your `index.html` page with a special redirect parameter. You would need to add a `404.html` file with the redirection code to the `build` folder before deploying your project, and you’ll need to add code handling the redirect parameter to `index.html`. You can find a detailed explanation of this technique [in this guide](https://github.com/rafrex/spa-github-pages).
|
||||
|
||||
#### Troubleshooting
|
||||
### Troubleshooting
|
||||
|
||||
##### "/dev/tty: No such a device or address"
|
||||
#### "/dev/tty: No such a device or address"
|
||||
|
||||
If, when deploying, you get `/dev/tty: No such a device or address` or a similar error, try the following:
|
||||
|
||||
@@ -345,7 +345,7 @@ If, when deploying, you get `/dev/tty: No such a device or address` or a similar
|
||||
2. `git remote set-url origin https://<user>:<token>@github.com/<user>/<repo>` .
|
||||
3. Try `npm run deploy` again
|
||||
|
||||
##### "Cannot read property 'email' of null"
|
||||
#### "Cannot read property 'email' of null"
|
||||
|
||||
If, when deploying, you get `Cannot read property 'email' of null`, try the following:
|
||||
|
||||
@@ -353,16 +353,16 @@ If, when deploying, you get `Cannot read property 'email' of null`, try the foll
|
||||
2. `git config --global user.email '<your_email>'`
|
||||
3. Try `npm run deploy` again
|
||||
|
||||
### [Heroku](https://www.heroku.com/)
|
||||
## [Heroku](https://www.heroku.com/)
|
||||
|
||||
Use the [Heroku Buildpack for Create React App](https://github.com/mars/create-react-app-buildpack).<br>
|
||||
You can find instructions in [Deploying React with Zero Configuration](https://blog.heroku.com/deploying-react-with-zero-configuration).
|
||||
|
||||
#### Resolving Heroku Deployment Errors
|
||||
### Resolving Heroku Deployment Errors
|
||||
|
||||
Sometimes `npm run build` works locally but fails during deploy via Heroku. Following are the most common cases.
|
||||
|
||||
##### "Module not found: Error: Cannot resolve 'file' or 'directory'"
|
||||
#### "Module not found: Error: Cannot resolve 'file' or 'directory'"
|
||||
|
||||
If you get something like this:
|
||||
|
||||
@@ -376,7 +376,7 @@ It means you need to ensure that the lettercase of the file or directory you `im
|
||||
|
||||
This is important because Linux (the operating system used by Heroku) is case sensitive. So `MyDirectory` and `mydirectory` are two distinct directories and thus, even though the project builds locally, the difference in case breaks the `import` statements on Heroku remotes.
|
||||
|
||||
##### "Could not find a required file."
|
||||
#### "Could not find a required file."
|
||||
|
||||
If you exclude or ignore necessary files from the package you will see a error similar this one:
|
||||
|
||||
@@ -391,7 +391,7 @@ remote: npm ERR! argv "/tmp/build_a2875fc163b209225122d68916f1d4df/.heroku/node/
|
||||
|
||||
In this case, ensure that the file is there with the proper lettercase and that’s not ignored on your local `.gitignore` or `~/.gitignore_global`.
|
||||
|
||||
### [Netlify](https://www.netlify.com/)
|
||||
## [Netlify](https://www.netlify.com/)
|
||||
|
||||
**To do a manual deploy to Netlify’s CDN:**
|
||||
|
||||
@@ -420,7 +420,7 @@ To support `pushState`, make sure to create a `public/_redirects` file with the
|
||||
|
||||
When you build the project, Create React App will place the `public` folder contents into the build output.
|
||||
|
||||
### [Now](https://zeit.co/now)
|
||||
## [Now](https://zeit.co/now)
|
||||
|
||||
Now offers a zero-configuration single-command deployment. You can use `now` to deploy your app for free.
|
||||
|
||||
@@ -440,11 +440,11 @@ Now offers a zero-configuration single-command deployment. You can use `now` to
|
||||
|
||||
Details are available in [this article.](https://zeit.co/blog/unlimited-static)
|
||||
|
||||
### [S3](https://aws.amazon.com/s3) and [CloudFront](https://aws.amazon.com/cloudfront/)
|
||||
## [S3](https://aws.amazon.com/s3) and [CloudFront](https://aws.amazon.com/cloudfront/)
|
||||
|
||||
See this [blog post](https://medium.com/@omgwtfmarc/deploying-create-react-app-to-s3-or-cloudfront-48dae4ce0af) on how to deploy your React app to Amazon Web Services S3 and CloudFront.
|
||||
|
||||
### [Surge](https://surge.sh/)
|
||||
## [Surge](https://surge.sh/)
|
||||
|
||||
Install the Surge CLI if you haven’t already by running `npm install -g surge`. Run the `surge` command and log in you or create a new account.
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ Create React App doesn’t include any tools for this by default, but you can ea
|
||||
|
||||
You can also deploy your Storybook or style guide as a static app. This way, everyone in your team can view and review different states of UI components without starting a backend server or creating an account in your app.
|
||||
|
||||
### Getting Started with Storybook
|
||||
## Getting Started with Storybook
|
||||
|
||||
Storybook is a development environment for React UI components. It allows you to browse a component library, view the different states of each component, and interactively develop and test components.
|
||||
|
||||
@@ -43,7 +43,7 @@ Learn more about React Storybook:
|
||||
- [GitHub Repo](https://github.com/storybooks/storybook)
|
||||
- [Snapshot Testing UI](https://github.com/storybooks/storybook/tree/master/addons/storyshots) with Storybook + addon/storyshot
|
||||
|
||||
### Getting Started with Styleguidist
|
||||
## Getting Started with Styleguidist
|
||||
|
||||
Styleguidist combines a style guide, where all your components are presented on a single page with their props documentation and usage examples, with an environment for developing components in isolation, similar to Storybook. In Styleguidist you write examples in Markdown, where each code snippet is rendered as a live editable playground.
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ While you can still use `require()` and `module.exports`, we encourage you to us
|
||||
|
||||
For example:
|
||||
|
||||
### `Button.js`
|
||||
## `Button.js`
|
||||
|
||||
```js
|
||||
import React, { Component } from 'react';
|
||||
@@ -22,7 +22,7 @@ class Button extends Component {
|
||||
export default Button; // Don’t forget to use export default!
|
||||
```
|
||||
|
||||
### `DangerButton.js`
|
||||
## `DangerButton.js`
|
||||
|
||||
```js
|
||||
import React, { Component } from 'react';
|
||||
|
||||
@@ -6,17 +6,17 @@ title: Integrating with an API Backend
|
||||
These tutorials will help you to integrate your app with an API backend running on another port,
|
||||
using `fetch()` to access it.
|
||||
|
||||
### Node
|
||||
## Node
|
||||
|
||||
Check out [this tutorial](https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/).
|
||||
You can find the companion GitHub repository [here](https://github.com/fullstackreact/food-lookup-demo).
|
||||
|
||||
### Ruby on Rails
|
||||
## Ruby on Rails
|
||||
|
||||
Check out [this tutorial](https://www.fullstackreact.com/articles/how-to-get-create-react-app-to-work-with-your-rails-api/).
|
||||
You can find the companion GitHub repository [here](https://github.com/fullstackreact/food-lookup-demo-rails).
|
||||
|
||||
### API Platform (PHP and Symfony)
|
||||
## API Platform (PHP and Symfony)
|
||||
|
||||
[API Platform](https://api-platform.com) is a framework designed to build API-driven projects.
|
||||
It allows to create hypermedia and GraphQL APIs in minutes.
|
||||
|
||||
@@ -22,7 +22,7 @@ serviceWorker.unregister();
|
||||
As the comment states, switching `serviceWorker.unregister()` to
|
||||
`serviceWorker.register()` will opt you in to using the service worker.
|
||||
|
||||
### Why Opt-in?
|
||||
## Why Opt-in?
|
||||
|
||||
Offline-first Progressive Web Apps are faster and more reliable than traditional web pages, and provide an engaging mobile experience:
|
||||
|
||||
@@ -42,7 +42,7 @@ for handling all requests for local assets, including
|
||||
for your HTML, ensuring that your web app is consistently fast, even on a slow
|
||||
or unreliable network.
|
||||
|
||||
### Offline-First Considerations
|
||||
## Offline-First Considerations
|
||||
|
||||
If you do decide to opt-in to service worker registration, please take the
|
||||
following into account:
|
||||
@@ -86,7 +86,7 @@ following into account:
|
||||
cross-origin traffic, like HTTP [API requests](/docs/integrating-with-an-api-backend),
|
||||
images, or embeds loaded from a different domain.
|
||||
|
||||
### Progressive Web App Metadata
|
||||
## Progressive Web App Metadata
|
||||
|
||||
The default configuration includes a web app manifest located at
|
||||
[`public/manifest.json`](public/manifest.json), that you can customize with
|
||||
|
||||
@@ -40,7 +40,7 @@ If the `proxy` option is **not** flexible enough for you, alternatively you can:
|
||||
- Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
|
||||
- Use [environment variables](/docs/adding-custom-environment-variables) to inject the right server host and port into your app.
|
||||
|
||||
### "Invalid Host Header" Errors After Configuring Proxy
|
||||
## "Invalid Host Header" Errors After Configuring Proxy
|
||||
|
||||
When you enable the `proxy` option, you opt into a more strict set of host checks. This is necessary because leaving the backend open to remote hosts makes your computer vulnerable to DNS rebinding attacks. The issue is explained in [this article](https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a) and [this issue](https://github.com/webpack/webpack-dev-server/issues/887).
|
||||
|
||||
@@ -66,7 +66,7 @@ DANGEROUSLY_DISABLE_HOST_CHECK=true
|
||||
|
||||
We don’t recommend this approach.
|
||||
|
||||
### Configuring the Proxy Manually
|
||||
## Configuring the Proxy Manually
|
||||
|
||||
> Note: this feature is available with `react-scripts@2.0.0` and higher.
|
||||
|
||||
|
||||
@@ -4,8 +4,6 @@ title: Running Tests
|
||||
sidebar_label: Running tests
|
||||
---
|
||||
|
||||
## Running Tests
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.3.0` and higher.<br>
|
||||
|
||||
> [Read the migration guide to learn how to enable it in older projects!](https://github.com/facebook/create-react-app/blob/master/CHANGELOG.md#migrating-from-023-to-030)
|
||||
@@ -18,7 +16,7 @@ While Jest provides browser globals such as `window` thanks to [jsdom](https://g
|
||||
|
||||
We recommend that you use a separate tool for browser end-to-end tests if you need them. They are beyond the scope of Create React App.
|
||||
|
||||
### Filename Conventions
|
||||
## Filename Conventions
|
||||
|
||||
Jest will look for test files with any of the following popular naming conventions:
|
||||
|
||||
@@ -30,7 +28,7 @@ The `.test.js` / `.spec.js` files (or the `__tests__` folders) can be located at
|
||||
|
||||
We recommend to put the test files (or `__tests__` folders) next to the code they are testing so that relative imports appear shorter. For example, if `App.test.js` and `App.js` are in the same folder, the test just needs to `import App from './App'` instead of a long relative path. Colocation also helps find tests more quickly in larger projects.
|
||||
|
||||
### Command Line Interface
|
||||
## Command Line Interface
|
||||
|
||||
When you run `npm test`, Jest will launch in the watch mode. Every time you save a file, it will re-run the tests, just like `npm start` recompiles the code.
|
||||
|
||||
@@ -38,7 +36,7 @@ The watcher includes an interactive command-line interface with the ability to r
|
||||
|
||||

|
||||
|
||||
### Version Control Integration
|
||||
## Version Control Integration
|
||||
|
||||
By default, when you run `npm test`, Jest will only run the tests related to files changed since the last commit. This is an optimization designed to make your tests run fast regardless of how many tests you have. However it assumes that you don’t often commit the code that doesn’t pass the tests.
|
||||
|
||||
@@ -46,7 +44,7 @@ Jest will always explicitly mention that it only ran tests related to the files
|
||||
|
||||
Jest will always run all tests on a [continuous integration](#continuous-integration) server or if the project is not inside a Git or Mercurial repository.
|
||||
|
||||
### Writing Tests
|
||||
## Writing Tests
|
||||
|
||||
To create tests, add `it()` (or `test()`) blocks with the name of the test and its code. You may optionally wrap them in `describe()` blocks for logical grouping but this is neither required nor recommended.
|
||||
|
||||
@@ -64,7 +62,7 @@ it('sums numbers', () => {
|
||||
All `expect()` matchers supported by Jest are [extensively documented here](https://facebook.github.io/jest/docs/en/expect.html#content).<br>
|
||||
You can also use [`jest.fn()` and `expect(fn).toBeCalled()`](https://facebook.github.io/jest/docs/en/expect.html#tohavebeencalled) to create “spies” or mock functions.
|
||||
|
||||
### Testing Components
|
||||
## Testing Components
|
||||
|
||||
There is a broad spectrum of component testing techniques. They range from a “smoke test” verifying that a component renders without throwing, to shallow rendering and testing some of the output, to full rendering and testing component lifecycle and state changes.
|
||||
|
||||
@@ -101,7 +99,7 @@ As of Enzyme 3, you will need to install Enzyme along with an Adapter correspond
|
||||
|
||||
The adapter will also need to be configured in your [global setup file](#initializing-test-environment):
|
||||
|
||||
#### `src/setupTests.js`
|
||||
### `src/setupTests.js`
|
||||
|
||||
```js
|
||||
import { configure } from 'enzyme';
|
||||
@@ -170,7 +168,7 @@ Import it in [`src/setupTests.js`](#initializing-test-environment) to make its m
|
||||
import 'jest-enzyme';
|
||||
```
|
||||
|
||||
### Using Third Party Assertion Libraries
|
||||
## Using Third Party Assertion Libraries
|
||||
|
||||
We recommend that you use `expect()` for assertions and `jest.fn()` for spies. If you are having issues with them please [file those against Jest](https://github.com/facebook/jest/issues/new), and we’ll fix them. We intend to keep making them better for React, supporting, for example, [pretty-printing React elements as JSX](https://github.com/facebook/jest/pull/1566).
|
||||
|
||||
@@ -183,7 +181,7 @@ import { expect } from 'chai';
|
||||
|
||||
and then use them in your tests like you normally do.
|
||||
|
||||
### Initializing Test Environment
|
||||
## Initializing Test Environment
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.4.0` and higher.
|
||||
|
||||
@@ -191,7 +189,7 @@ If your app uses a browser API that you need to mock in your tests or if you jus
|
||||
|
||||
For example:
|
||||
|
||||
#### `src/setupTests.js`
|
||||
### `src/setupTests.js`
|
||||
|
||||
```js
|
||||
const localStorageMock = {
|
||||
@@ -211,12 +209,12 @@ global.localStorage = localStorageMock;
|
||||
> }
|
||||
> ```
|
||||
|
||||
### Focusing and Excluding Tests
|
||||
## Focusing and Excluding Tests
|
||||
|
||||
You can replace `it()` with `xit()` to temporarily exclude a test from being executed.<br>
|
||||
Similarly, `fit()` lets you focus on a specific test without running any other tests.
|
||||
|
||||
### Coverage Reporting
|
||||
## Coverage Reporting
|
||||
|
||||
Jest has an integrated coverage reporter that works well with ES6 and requires no configuration.<br>
|
||||
Run `npm test -- --coverage` (note extra `--` in the middle) to include a coverage report like this:
|
||||
@@ -225,7 +223,7 @@ Run `npm test -- --coverage` (note extra `--` in the middle) to include a covera
|
||||
|
||||
Note that tests run much slower with coverage so it is recommended to run it separately from your normal workflow.
|
||||
|
||||
#### Configuration
|
||||
### Configuration
|
||||
|
||||
The default Jest coverage configuration can be overridden by adding any of the following supported keys to a Jest config in your package.json.
|
||||
|
||||
@@ -261,7 +259,7 @@ Example package.json:
|
||||
}
|
||||
```
|
||||
|
||||
### Continuous Integration
|
||||
## Continuous Integration
|
||||
|
||||
By default `npm test` runs the watcher with interactive CLI. However, you can force it to run tests once and finish the process by setting an environment variable called `CI`.
|
||||
|
||||
@@ -269,9 +267,9 @@ When creating a build of your application with `npm run build` linter warnings a
|
||||
|
||||
Popular CI servers already set the environment variable `CI` by default but you can do this yourself too:
|
||||
|
||||
### On CI servers
|
||||
## On CI servers
|
||||
|
||||
#### Travis CI
|
||||
### Travis CI
|
||||
|
||||
1. Following the [Travis Getting started](https://docs.travis-ci.com/user/getting-started/) guide for syncing your GitHub repository with Travis. You may need to initialize some settings manually in your [profile](https://travis-ci.org/profile) page.
|
||||
1. Add a `.travis.yml` file to your git repository.
|
||||
@@ -291,13 +289,13 @@ script:
|
||||
1. Trigger your first build with a git push.
|
||||
1. [Customize your Travis CI Build](https://docs.travis-ci.com/user/customizing-the-build/) if needed.
|
||||
|
||||
#### CircleCI
|
||||
### CircleCI
|
||||
|
||||
Follow [this article](https://medium.com/@knowbody/circleci-and-zeits-now-sh-c9b7eebcd3c1) to set up CircleCI with a Create React App project.
|
||||
|
||||
### On your own environment
|
||||
## On your own environment
|
||||
|
||||
##### Windows (cmd.exe)
|
||||
#### Windows (cmd.exe)
|
||||
|
||||
```cmd
|
||||
set CI=true&&npm test
|
||||
@@ -309,7 +307,7 @@ set CI=true&&npm run build
|
||||
|
||||
(Note: the lack of whitespace is intentional.)
|
||||
|
||||
##### Windows (Powershell)
|
||||
#### Windows (Powershell)
|
||||
|
||||
```Powershell
|
||||
($env:CI = $true) -and (npm test)
|
||||
@@ -319,7 +317,7 @@ set CI=true&&npm run build
|
||||
($env:CI = $true) -and (npm run build)
|
||||
```
|
||||
|
||||
##### Linux, macOS (Bash)
|
||||
#### Linux, macOS (Bash)
|
||||
|
||||
```bash
|
||||
CI=true npm test
|
||||
@@ -335,7 +333,7 @@ The test command will force Jest to run tests once instead of launching the watc
|
||||
|
||||
The build command will check for linter warnings and fail if any are found.
|
||||
|
||||
### Disabling jsdom
|
||||
## Disabling jsdom
|
||||
|
||||
If you know that none of your tests depend on [jsdom](https://github.com/tmpvar/jsdom), you can safely set `--env=node`, and your tests will run faster:
|
||||
|
||||
@@ -361,11 +359,11 @@ In contrast, **jsdom is not needed** for the following APIs:
|
||||
|
||||
Finally, jsdom is also not needed for [snapshot testing](http://facebook.github.io/jest/blog/2016/07/27/jest-14.html).
|
||||
|
||||
### Snapshot Testing
|
||||
## Snapshot Testing
|
||||
|
||||
Snapshot testing is a feature of Jest that automatically generates text snapshots of your components and saves them on the disk so if the UI output changes, you get notified without manually writing any assertions on the component output. [Read more about snapshot testing.](http://facebook.github.io/jest/blog/2016/07/27/jest-14.html)
|
||||
|
||||
### Editor Integration
|
||||
## Editor Integration
|
||||
|
||||
If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest extension](https://github.com/orta/vscode-jest) which works with Create React App out of the box. This provides a lot of IDE-like features while using a text editor: showing the status of a test run with potential fail messages inline, starting and stopping the watcher automatically, and offering one-click snapshot updates.
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ title: Troubleshooting
|
||||
sidebar_label: Troubleshooting
|
||||
---
|
||||
|
||||
### `npm start` doesn’t detect changes
|
||||
## `npm start` doesn’t detect changes
|
||||
|
||||
When you save a file while `npm start` is running, the browser should refresh with the updated code.<br>
|
||||
If this doesn’t happen, try one of the following workarounds:
|
||||
@@ -18,7 +18,7 @@ If this doesn’t happen, try one of the following workarounds:
|
||||
|
||||
If none of these solutions help please leave a comment [in this thread](https://github.com/facebook/create-react-app/issues/659).
|
||||
|
||||
### `npm test` hangs or crashes on macOS Sierra
|
||||
## `npm test` hangs or crashes on macOS Sierra
|
||||
|
||||
If you run `npm test` and the console gets stuck after printing `react-scripts test` to the console there might be a problem with your [Watchman](https://facebook.github.io/watchman/) installation as described in [facebook/create-react-app#713](https://github.com/facebook/create-react-app/issues/713).
|
||||
|
||||
@@ -42,7 +42,7 @@ If this still doesn’t help, try running `launchctl unload -F ~/Library/LaunchA
|
||||
|
||||
There are also reports that _uninstalling_ Watchman fixes the issue. So if nothing else helps, remove it from your system and try again.
|
||||
|
||||
### `npm run build` exits too early
|
||||
## `npm run build` exits too early
|
||||
|
||||
It is reported that `npm run build` can fail on machines with limited memory and no swap space, which is common in cloud environments. Even with small projects this command can increase RAM usage in your system by hundreds of megabytes, so if you have less than 1 GB of available memory your build is likely to fail with the following message:
|
||||
|
||||
@@ -50,12 +50,12 @@ It is reported that `npm run build` can fail on machines with limited memory and
|
||||
|
||||
If you are completely sure that you didn't terminate the process, consider [adding some swap space](https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04) to the machine you’re building on, or build the project locally.
|
||||
|
||||
### `npm run build` fails on Heroku
|
||||
## `npm run build` fails on Heroku
|
||||
|
||||
This may be a problem with case sensitive filenames.
|
||||
Please refer to [this section](/docs/deployment#resolving-heroku-deployment-errors).
|
||||
|
||||
### Moment.js locales are missing
|
||||
## Moment.js locales are missing
|
||||
|
||||
If you use a [Moment.js](https://momentjs.com/), you might notice that only the English locale is available by default. This is because the locale files are large, and you probably only need a subset of [all the locales provided by Moment.js](https://momentjs.com/#multiple-locale-support).
|
||||
|
||||
@@ -81,7 +81,7 @@ moment.locale('fr');
|
||||
|
||||
This will only work for locales that have been explicitly imported before.
|
||||
|
||||
### `npm run build` fails to minify
|
||||
## `npm run build` fails to minify
|
||||
|
||||
Before `react-scripts@2.0.0`, this problem was caused by third party `node_modules` using modern JavaScript features because the minifier couldn't handle them during the build. This has been solved by compiling standard modern JavaScript features inside `node_modules` in `react-scripts@2.0.0` and higher.
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ You may require the dev server to serve pages over HTTPS. One particular case wh
|
||||
|
||||
To do this, set the `HTTPS` environment variable to `true`, then start the dev server as usual with `npm start`:
|
||||
|
||||
#### Windows (cmd.exe)
|
||||
### Windows (cmd.exe)
|
||||
|
||||
```cmd
|
||||
set HTTPS=true&&npm start
|
||||
@@ -17,13 +17,13 @@ set HTTPS=true&&npm start
|
||||
|
||||
(Note: the lack of whitespace is intentional.)
|
||||
|
||||
#### Windows (Powershell)
|
||||
### Windows (Powershell)
|
||||
|
||||
```Powershell
|
||||
($env:HTTPS = $true) -and (npm start)
|
||||
```
|
||||
|
||||
#### Linux, macOS (Bash)
|
||||
### Linux, macOS (Bash)
|
||||
|
||||
```bash
|
||||
HTTPS=true npm start
|
||||
|
||||
@@ -5,12 +5,12 @@ title: Using the `public` Folder
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.5.0` and higher.
|
||||
|
||||
### Changing the HTML
|
||||
## Changing the HTML
|
||||
|
||||
The `public` folder contains the HTML file so you can tweak it, for example, to [set the page title](/docs/changing-the-page-title).
|
||||
The `<script>` tag with the compiled code will be added to it automatically during the build process.
|
||||
|
||||
### Adding Assets Outside of the Module System
|
||||
## Adding Assets Outside of the Module System
|
||||
|
||||
You can also add other assets to the `public` folder.
|
||||
|
||||
@@ -53,7 +53,7 @@ Keep in mind the downsides of this approach:
|
||||
- Missing files will not be called at compilation time, and will cause 404 errors for your users.
|
||||
- Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
|
||||
|
||||
### When to Use the `public` Folder
|
||||
## When to Use the `public` Folder
|
||||
|
||||
Normally we recommend importing [stylesheets](/docs/adding-a-stylesheet), [images, and fonts](/docs/adding-images-fonts-and-files) from JavaScript.
|
||||
The `public` folder is useful as a workaround for a number of less common cases:
|
||||
|
||||
Reference in New Issue
Block a user