Skip to content

Commit f0047bf

Browse files
authored
Merge pull request #5347 from selbekk/port-user-guide-over
Port user guide over
2 parents 63e7fff + 6fc577c commit f0047bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2727
-20
lines changed

docusaurus/docs/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
id: adding-a-css-modules-stylesheet
3+
title: Adding a CSS Modules Stylesheet
4+
---
5+
6+
> Note: this feature is available with `[email protected]` and higher.
7+
8+
This project supports [CSS Modules](https://github.com/css-modules/css-modules) alongside regular stylesheets using the `[name].module.css` file naming convention. CSS Modules allows the scoping of CSS by automatically creating a unique classname of the format `[filename]\_[classname]\_\_[hash]`.
9+
10+
> **Tip:** Should you want to preprocess a stylesheet with Sass then make sure to [follow the installation instructions](/docs/adding-a-sass-stylesheet) and then change the stylesheet file extension as follows: `[name].module.scss` or `[name].module.sass`.
11+
12+
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/).
13+
14+
## `Button.module.css`
15+
16+
```css
17+
.error {
18+
background-color: red;
19+
}
20+
```
21+
22+
## `another-stylesheet.css`
23+
24+
```css
25+
.error {
26+
color: red;
27+
}
28+
```
29+
30+
## `Button.js`
31+
32+
```js
33+
import React, { Component } from 'react';
34+
import styles from './Button.module.css'; // Import css modules stylesheet as styles
35+
import './another-stylesheet.css'; // Import regular stylesheet
36+
37+
class Button extends Component {
38+
render() {
39+
// reference as a js object
40+
return <button className={styles.error}>Error Button</button>;
41+
}
42+
}
43+
```
44+
45+
## Result
46+
47+
No clashes from other `.error` class names
48+
49+
```html
50+
<!-- This button has red background but not red text -->
51+
<button class="Button_error_ax7yz"></div>
52+
```
53+
54+
**This is an optional feature.** Regular `<link>` stylesheets and CSS files are fully supported. CSS Modules are turned on for files ending with the `.module.css` extension.

docusaurus/docs/adding-a-router.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
id: adding-a-router
3+
title: Adding a Router
4+
---
5+
6+
Create React App doesn't prescribe a specific routing solution, but [React Router](https://reacttraining.com/react-router/web/) is the most popular one.
7+
8+
To add it, run:
9+
10+
```sh
11+
npm install --save react-router-dom
12+
```
13+
14+
Alternatively you may use `yarn`:
15+
16+
```sh
17+
yarn add react-router-dom
18+
```
19+
20+
To try it, delete all the code in `src/App.js` and replace it with any of the examples on its website. The [Basic Example](https://reacttraining.com/react-router/web/example/basic) is a good place to get started.
21+
22+
Note that [you may need to configure your production server to support client-side routing](/docs/deployments#serving-apps-with-client-side-routing) before deploying your app.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
id: adding-a-sass-stylesheet
3+
title: Adding a Sass Stylesheet
4+
---
5+
6+
> Note: this feature is available with `[email protected]` and higher.
7+
8+
Generally, we recommend that you don’t reuse the same CSS classes across different components. For example, instead of using a `.Button` CSS class in `<AcceptButton>` and `<RejectButton>` components, we recommend creating a `<Button>` component with its own `.Button` styles, that both `<AcceptButton>` and `<RejectButton>` can render (but [not inherit](https://facebook.github.io/react/docs/composition-vs-inheritance.html)).
9+
10+
Following this rule often makes CSS preprocessors less useful, as features like mixins and nesting are replaced by component composition. You can, however, integrate a CSS preprocessor if you find it valuable.
11+
12+
To use Sass, first install `node-sass`:
13+
14+
```bash
15+
$ npm install node-sass --save
16+
$ # or
17+
$ yarn add node-sass
18+
```
19+
20+
Now you can rename `src/App.css` to `src/App.scss` and update `src/App.js` to import `src/App.scss`.
21+
This file and any other file will be automatically compiled if imported with the extension `.scss` or `.sass`.
22+
23+
To share variables between Sass files, you can use Sass imports. For example, `src/App.scss` and other component style files could include `@import "./shared.scss";` with variable definitions.
24+
25+
This will allow you to do imports like
26+
27+
```scss
28+
@import 'styles/_colors.scss'; // assuming a styles directory under src/
29+
@import '~nprogress/nprogress'; // importing a css file from the nprogress node module
30+
```
31+
32+
> **Tip:** You can opt into using this feature with [CSS modules](/docs/adding-a-css-modules-stylesheet) too!
33+
34+
> **Note:** You must prefix imports from `node_modules` with `~` as displayed above.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
id: adding-a-stylesheet
3+
title: Adding a Stylesheet
4+
---
5+
6+
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**:
7+
8+
## `Button.css`
9+
10+
```css
11+
.Button {
12+
padding: 20px;
13+
}
14+
```
15+
16+
## `Button.js`
17+
18+
```js
19+
import React, { Component } from 'react';
20+
import './Button.css'; // Tell Webpack that Button.js uses these styles
21+
22+
class Button extends Component {
23+
render() {
24+
// You can use them as regular CSS styles
25+
return <div className="Button" />;
26+
}
27+
}
28+
```
29+
30+
**This is not required for React** but many people find this feature convenient. You can read about the benefits of this approach [here](https://medium.com/seek-blog/block-element-modifying-your-javascript-components-d7f99fcab52b). However you should be aware that this makes your code less portable to other build tools and environments than Webpack.
31+
32+
In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified `.css` file in the build output.
33+
34+
If you are concerned about using Webpack-specific semantics, you can put all your CSS right into `src/index.css`. It would still be imported from `src/index.js`, but you could always remove that import if you later migrate to a different build tool.

docusaurus/docs/adding-bootstrap.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: adding-bootstrap
3+
title: Adding Bootstrap
4+
---
5+
6+
You don’t have to use [reactstrap](https://reactstrap.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:
7+
8+
Install reactstrap and Bootstrap from npm. reactstrap does not include Bootstrap CSS so this needs to be installed as well:
9+
10+
```sh
11+
npm install --save reactstrap bootstrap@4
12+
```
13+
14+
Alternatively you may use `yarn`:
15+
16+
```sh
17+
yarn add bootstrap@4 reactstrap
18+
```
19+
20+
Import Bootstrap CSS and optionally Bootstrap theme CSS in the beginning of your `src/index.js` file:
21+
22+
```js
23+
import 'bootstrap/dist/css/bootstrap.css';
24+
// Put any other imports below so that CSS from your
25+
// components takes precedence over default styles.
26+
```
27+
28+
Import required reactstrap components within `src/App.js` file or your custom component files:
29+
30+
```js
31+
import { Button } from 'reactstrap';
32+
```
33+
34+
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.
35+
36+
## Using a Custom Theme
37+
38+
> Note: this feature is available with `[email protected]` and higher.
39+
40+
Sometimes you might need to tweak the visual styles of Bootstrap (or equivalent package).<br>
41+
As of `[email protected]` you can import `.scss` files. This makes it possible to use a package's built-in Sass variables for global style preferences.
42+
43+
To customize Bootstrap, create a file called `src/custom.scss` (or similar) and import the Bootstrap source stylesheet. Add any overrides _before_ the imported file(s). You can reference [Bootstrap's documentation](http://getbootstrap.com/docs/4.1/getting-started/theming/#css-variables) for the names of the available variables.
44+
45+
```scss
46+
// Override default variables before the import
47+
$body-bg: #000;
48+
49+
// Import Bootstrap and its default variables
50+
@import '~bootstrap/scss/bootstrap.scss';
51+
```
52+
53+
> **Note:** You must prefix imports from `node_modules` with `~` as displayed above.
54+
55+
Finally, import the newly created `.scss` file instead of the default Bootstrap `.css` in the beginning of your `src/index.js` file, for example:
56+
57+
```javascript
58+
import './custom.scss';
59+
```
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
id: adding-custom-environment-variables
3+
title: Adding Custom Environment Variables
4+
---
5+
> Note: this feature is available with `[email protected]` and higher.
6+
7+
Your project can consume variables declared in your environment as if they were declared locally in your JS files. By
8+
default you will have `NODE_ENV` defined for you, and any other environment variables starting with
9+
`REACT_APP_`.
10+
11+
**The environment variables are embedded during the build time**. Since Create React App produces a static HTML/CSS/JS bundle, it can’t possibly read them at runtime. To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime, just like [described here](/docs/injecting-data-from-the-server-into-the-page). Alternatively you can rebuild the app on the server anytime you change them.
12+
13+
> Note: You must create custom environment variables beginning with `REACT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid accidentally [exposing a private key on the machine that could have the same name](https://github.com/facebook/create-react-app/issues/865#issuecomment-252199527). Changing any environment variables will require you to restart the development server if it is running.
14+
15+
These environment variables will be defined for you on `process.env`. For example, having an environment
16+
variable named `REACT_APP_SECRET_CODE` will be exposed in your JS as `process.env.REACT_APP_SECRET_CODE`.
17+
18+
There is also a special built-in environment variable called `NODE_ENV`. You can read it from `process.env.NODE_ENV`. When you run `npm start`, it is always equal to `'development'`, when you run `npm test` it is always equal to `'test'`, and when you run `npm run build` to make a production bundle, it is always equal to `'production'`. **You cannot override `NODE_ENV` manually.** This prevents developers from accidentally deploying a slow development build to production.
19+
20+
These environment variables can be useful for displaying information conditionally based on where the project is
21+
deployed or consuming sensitive data that lives outside of version control.
22+
23+
First, you need to have environment variables defined. For example, let’s say you wanted to consume a secret defined
24+
in the environment inside a `<form>`:
25+
26+
```jsx
27+
render() {
28+
return (
29+
<div>
30+
<small>You are running this application in <b>{process.env.NODE_ENV}</b> mode.</small>
31+
<form>
32+
<input type="hidden" defaultValue={process.env.REACT_APP_SECRET_CODE} />
33+
</form>
34+
</div>
35+
);
36+
}
37+
```
38+
39+
During the build, `process.env.REACT_APP_SECRET_CODE` will be replaced with the current value of the `REACT_APP_SECRET_CODE` environment variable. Remember that the `NODE_ENV` variable will be set for you automatically.
40+
41+
When you load the app in the browser and inspect the `<input>`, you will see its value set to `abcdef`, and the bold text will show the environment provided when using `npm start`:
42+
43+
```html
44+
<div>
45+
<small>You are running this application in <b>development</b> mode.</small>
46+
<form>
47+
<input type="hidden" value="abcdef" />
48+
</form>
49+
</div>
50+
```
51+
52+
The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the environment. In order to consume this
53+
value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in
54+
a `.env` file. Both of these ways are described in the next few sections.
55+
56+
Having access to the `NODE_ENV` is also useful for performing actions conditionally:
57+
58+
```js
59+
if (process.env.NODE_ENV !== 'production') {
60+
analytics.disable();
61+
}
62+
```
63+
64+
When you compile the app with `npm run build`, the minification step will strip out this condition, and the resulting bundle will be smaller.
65+
66+
## Referencing Environment Variables in the HTML
67+
68+
> Note: this feature is available with `[email protected]` and higher.
69+
70+
You can also access the environment variables starting with `REACT_APP_` in the `public/index.html`. For example:
71+
72+
```html
73+
<title>%REACT_APP_WEBSITE_NAME%</title>
74+
```
75+
76+
Note that the caveats from the above section apply:
77+
78+
- Apart from a few built-in variables (`NODE_ENV` and `PUBLIC_URL`), variable names must start with `REACT_APP_` to work.
79+
- 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).
80+
81+
## Adding Temporary Environment Variables In Your Shell
82+
83+
Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the
84+
life of the shell session.
85+
86+
### Windows (cmd.exe)
87+
88+
```cmd
89+
set "REACT_APP_SECRET_CODE=abcdef" && npm start
90+
```
91+
92+
(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.)
93+
94+
### Windows (Powershell)
95+
96+
```Powershell
97+
($env:REACT_APP_SECRET_CODE = "abcdef") -and (npm start)
98+
```
99+
100+
### Linux, macOS (Bash)
101+
102+
```bash
103+
REACT_APP_SECRET_CODE=abcdef npm start
104+
```
105+
106+
## Adding Development Environment Variables In `.env`
107+
108+
> Note: this feature is available with `[email protected]` and higher.
109+
110+
To define permanent environment variables, create a file called `.env` in the root of your project:
111+
112+
```
113+
REACT_APP_SECRET_CODE=abcdef
114+
```
115+
116+
> Note: You must create custom environment variables beginning with `REACT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid [accidentally exposing a private key on the machine that could have the same name](https://github.com/facebook/create-react-app/issues/865#issuecomment-252199527). Changing any environment variables will require you to restart the development server if it is running.
117+
118+
`.env` files **should be** checked into source control (with the exclusion of `.env*.local`).
119+
120+
### What other `.env` files can be used?
121+
122+
> Note: this feature is **available with `[email protected]` and higher**.
123+
124+
- `.env`: Default.
125+
- `.env.local`: Local overrides. **This file is loaded for all environments except test.**
126+
- `.env.development`, `.env.test`, `.env.production`: Environment-specific settings.
127+
- `.env.development.local`, `.env.test.local`, `.env.production.local`: Local overrides of environment-specific settings.
128+
129+
Files on the left have more priority than files on the right:
130+
131+
- `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env`
132+
- `npm run build`: `.env.production.local`, `.env.production`, `.env.local`, `.env`
133+
- `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing)
134+
135+
These variables will act as the defaults if the machine does not explicitly set them.<br>
136+
Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details.
137+
138+
> Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need
139+
> 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).
140+
141+
### Expanding Environment Variables In `.env`
142+
143+
> Note: this feature is available with `[email protected]` and higher.
144+
145+
Expand variables already on your machine for use in your `.env` file (using [dotenv-expand](https://github.com/motdotla/dotenv-expand)).
146+
147+
For example, to get the environment variable `npm_package_version`:
148+
149+
```
150+
REACT_APP_VERSION=$npm_package_version
151+
# also works:
152+
# REACT_APP_VERSION=${npm_package_version}
153+
```
154+
155+
Or expand variables local to the current `.env` file:
156+
157+
```
158+
DOMAIN=www.example.com
159+
REACT_APP_FOO=$DOMAIN/foo
160+
REACT_APP_BAR=$DOMAIN/bar
161+
```

docusaurus/docs/adding-flow.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
id: adding-flow
3+
title: Adding Flow
4+
---
5+
6+
Flow is a static type checker that helps you write code with fewer bugs. Check out this [introduction to using static types in JavaScript](https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-1-8382da1e0adb) if you are new to this concept.
7+
8+
Recent versions of [Flow](https://flow.org/) work with Create React App projects out of the box.
9+
10+
To add Flow to a Create React App project, follow these steps:
11+
12+
1. Run `npm install --save flow-bin` (or `yarn add flow-bin`).
13+
2. Add `"flow": "flow"` to the `scripts` section of your `package.json`.
14+
3. Run `npm run flow init` (or `yarn flow init`) to create a [`.flowconfig` file](https://flow.org/en/docs/config/) in the root directory.
15+
4. Add `// @flow` to any files you want to type check (for example, to `src/App.js`).
16+
17+
Now you can run `npm run flow` (or `yarn flow`) to check the files for type errors.
18+
You can optionally use an IDE like [Nuclide](https://nuclide.io/docs/languages/flow/) for a better integrated experience.
19+
In the future we plan to integrate it into Create React App even more closely.
20+
21+
To learn more about Flow, check out [its documentation](https://flow.org/).

0 commit comments

Comments
 (0)