Skip to content

Commit 6435d5b

Browse files
authored
Add basic How To
1 parent 300cdc7 commit 6435d5b

File tree

1 file changed

+176
-2
lines changed

1 file changed

+176
-2
lines changed

template/README.md

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
## Getting Started
1+
Below you will find some information on how to perform common tasks.
2+
You can find the most recent version of this guide [here](https://github.com/facebookincubator/create-react-app/blob/master/template/README.md).
23

3-
Inside that directory, you will find some helpful scripts!
4+
## Sending Feedback
5+
6+
We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues).
7+
8+
## Available Scripts
9+
10+
In this directory, you can run:
411

512
### `npm start`
613

@@ -27,3 +34,170 @@ If you aren’t satisfied with the build tool and configuration choices, you can
2734
Instead, it will copy all the configuration files and the transient dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
2835

2936
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
37+
38+
## How To...
39+
40+
### Import a Component
41+
42+
This project setup supports ES6 modules thanks to Babel.
43+
While you can still use `require()` and `module.exports`, we encourage you to use [`import` and `export`](http://exploringjs.com/es6/ch_modules.html) instead.
44+
45+
For example:
46+
47+
### `Button.js`
48+
49+
```js
50+
import React, { Component } from 'react';
51+
52+
class Button extends Component {
53+
render() {
54+
// ...
55+
}
56+
}
57+
58+
export default Button; // Don’t forget to use export default!
59+
```
60+
61+
### `DangerButton.js`
62+
63+
```js
64+
import React, { Component } from 'react';
65+
import Button from './Button'; // Import a component from another file
66+
67+
class DangerButton extends Component {
68+
render() {
69+
return <Button color='red' />;
70+
}
71+
}
72+
73+
export default DangerButton;
74+
```
75+
76+
Be aware of the [difference between default and named exports](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281). It is a common source of mistakes.
77+
78+
We suggest that you stick to using default imports and exports when a module only exports a single thing (for example, a component). That’s what you get when you use `export default Button` and `import Button from './Button'`.
79+
80+
Named exports are useful for utility modules that export several functions. A module may have at most one default export and as many named exports as you like.
81+
82+
Learn more about ES6 modules:
83+
84+
* [When to use the curly braces?](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281)
85+
* [Exploring ES6: Modules](http://exploringjs.com/es6/ch_modules.html)
86+
* [Understanding ES6: Modules](https://leanpub.com/understandinges6/read#leanpub-auto-encapsulating-code-with-modules)
87+
88+
### Add a Stylesheet
89+
90+
This project setup uses [Webpack](https://webpack.github.io/) for handling all assets.
91+
Webpack offers a custom way of “extending” the concept of `import` beyond JavaScript.
92+
93+
To express that a JavaScript file depends on a CSS file, you need to import it from the JavaScript file:
94+
95+
#### `Button.css`
96+
97+
```css
98+
.Button {
99+
padding: 20px;
100+
}
101+
```
102+
103+
#### `Button.js`
104+
105+
```js
106+
import React, { Component } from 'react';
107+
import './Button.css'; // Tell Webpack that Button.js uses these styles
108+
109+
class Button extends Component {
110+
render() {
111+
// You can use them as regular CSS styles
112+
return <div className='Button' />;
113+
}
114+
}
115+
```
116+
117+
**This is not required for React** but many people find this feature convenient.
118+
However be aware that this makes your code less portable to other build tools and environments than Webpack.
119+
120+
In development, this allows your styles to be reloaded on the fly as you edit them.
121+
In production, all CSS files will be concatenated into a single minified `.css` file in the build output.
122+
123+
You can read about the benefits of this approach [here](https://medium.com/seek-ui-engineering/block-element-modifying-your-javascript-components-d7f99fcab52b).
124+
125+
However **you are welcome to ignore it and put all your CSS in `src/index.css` if you prefer so.**
126+
It is imported from `src/index.js`, and you can always remove that import if you migrate to a different build tool.
127+
128+
### Post-Process CSS
129+
130+
This project setup minifies your CSS and adds vendor prefixes to it automatically through [Autoprefixer](https://github.com/postcss/autoprefixer) so you don’t need to worry about it.
131+
132+
For example, this:
133+
134+
```css
135+
.App {
136+
display: flex;
137+
flex-direction: row;
138+
align-items: center;
139+
}
140+
```
141+
142+
becomes this:
143+
144+
```css
145+
.App {
146+
display: -webkit-box;
147+
display: -ms-flexbox;
148+
display: flex;
149+
-webkit-box-orient: horizontal;
150+
-webkit-box-direction: normal;
151+
-ms-flex-direction: row;
152+
flex-direction: row;
153+
-webkit-box-align: center;
154+
-ms-flex-align: center;
155+
align-items: center;
156+
}
157+
```
158+
159+
There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
160+
161+
### Add Images and Fonts
162+
163+
With Webpack, using static assets like images and fonts works similarly to CSS.
164+
165+
You can `import` an image right in a JavaScript. This tells Webpack to include that image in the bundle.
166+
The *result* of the import will be the final image filename in the compiled bundle.
167+
168+
Here is an example:
169+
170+
```js
171+
import React from 'react';
172+
import logo from './logo.png'; // Tell Webpack this JS file uses this image
173+
174+
console.log(logo); // /84287d09b8053c6fa598893b8910786a.png
175+
176+
function Header() {
177+
// Import result is the URL of your image
178+
return <img src={logo} alt="Logo" />;
179+
}
180+
181+
export default function Header;
182+
```
183+
184+
You can also use images in CSS with relative module paths:
185+
186+
```css
187+
.Logo {
188+
background-image: url(./logo.png);
189+
}
190+
```
191+
192+
Webpack will thefind relative module references in CSS (they start with `./`) and replace them with the final paths in the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module.
193+
194+
The filenames are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.
195+
196+
Please be advised that this is also a custom feature of Webpack.
197+
**It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).
198+
199+
If you’d prefer to add and reference static assets in a more traditional way outside the module system, please let us know [in this issue](https://github.com/facebookincubator/create-react-app/issues/28), and we will add support for this.
200+
201+
### Something Missing?
202+
203+
If you have ideas for more “How To” recipes that should be on this page, [let us know](https://github.com/facebookincubator/create-react-app/issues) or [contribute some!](https://github.com/facebookincubator/create-react-app/edit/master/template/README.md)

0 commit comments

Comments
 (0)