Commit 6435d5bd authored by Dan Abramov's avatar Dan Abramov Committed by GitHub
Browse files

Add basic How To

parent 300cdc7d
No related merge requests found
Showing with 176 additions and 2 deletions
+176 -2
## Getting Started
Below you will find some information on how to perform common tasks.
You can find the most recent version of this guide [here](https://github.com/facebookincubator/create-react-app/blob/master/template/README.md).
Inside that directory, you will find some helpful scripts!
## Sending Feedback
We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues).
## Available Scripts
In this directory, you can run:
### `npm start`
......@@ -27,3 +34,170 @@ If you aren’t satisfied with the build tool and configuration choices, you can
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.
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.
## How To...
### Import a Component
This project setup supports ES6 modules thanks to Babel.
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.
For example:
### `Button.js`
```js
import React, { Component } from 'react';
class Button extends Component {
render() {
// ...
}
}
export default Button; // Don’t forget to use export default!
```
### `DangerButton.js`
```js
import React, { Component } from 'react';
import Button from './Button'; // Import a component from another file
class DangerButton extends Component {
render() {
return <Button color='red' />;
}
}
export default DangerButton;
```
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.
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'`.
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.
Learn more about ES6 modules:
* [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)
* [Exploring ES6: Modules](http://exploringjs.com/es6/ch_modules.html)
* [Understanding ES6: Modules](https://leanpub.com/understandinges6/read#leanpub-auto-encapsulating-code-with-modules)
### Add a Stylesheet
This project setup uses [Webpack](https://webpack.github.io/) 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 it from the JavaScript file:
#### `Button.css`
```css
.Button {
padding: 20px;
}
```
#### `Button.js`
```js
import React, { Component } from 'react';
import './Button.css'; // Tell Webpack that Button.js uses these styles
class Button extends Component {
render() {
// You can use them as regular CSS styles
return <div className='Button' />;
}
}
```
**This is not required for React** but many people find this feature convenient.
However be aware that this makes your code less portable to other build tools and environments than Webpack.
In development, this 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.
You can read about the benefits of this approach [here](https://medium.com/seek-ui-engineering/block-element-modifying-your-javascript-components-d7f99fcab52b).
However **you are welcome to ignore it and put all your CSS in `src/index.css` if you prefer so.**
It is imported from `src/index.js`, and you can always remove that import if you migrate to a different build tool.
### Post-Process CSS
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.
For example, this:
```css
.App {
display: flex;
flex-direction: row;
align-items: center;
}
```
becomes this:
```css
.App {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
```
There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
### Add Images and Fonts
With Webpack, using static assets like images and fonts works similarly to CSS.
You can `import` an image right in a JavaScript. This tells Webpack to include that image in the bundle.
The *result* of the import will be the final image filename in the compiled bundle.
Here is an example:
```js
import React from 'react';
import logo from './logo.png'; // Tell Webpack this JS file uses this image
console.log(logo); // /84287d09b8053c6fa598893b8910786a.png
function Header() {
// Import result is the URL of your image
return <img src={logo} alt="Logo" />;
}
export default function Header;
```
You can also use images in CSS with relative module paths:
```css
.Logo {
background-image: url(./logo.png);
}
```
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.
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.
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).
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.
### Something Missing?
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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment