Skip to content

Commit 822352c

Browse files
authored
Merge pull request #45 from HecFranco/master
#44 reviewed and readme_es file created
2 parents 19253a4 + e6f44fa commit 822352c

File tree

8 files changed

+234
-98
lines changed

8 files changed

+234
-98
lines changed

02 Properties/.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false
7+
}
8+
],
9+
"react"
10+
]
11+
}

02 Properties/package.json

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
{
2-
"name": "samplereact-es6",
2+
"name": "sample-es6",
33
"version": "1.0.0",
4-
"description": "A little project to provide a set of step by step guided samples using React and ES6",
4+
"description": "In this sample we are going to setup the basic plumbing to \"build\" our project and launch it in a dev server.",
55
"main": "index.js",
66
"scripts": {
7-
"start": "webpack-dev-server --inline",
7+
"start": "webpack-dev-server --mode development --inline --hot --open",
8+
"build": "webpack --mode development",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
1011
"author": "",
11-
"license": "MIT",
12-
"dependencies": {
13-
"bootstrap": "^3.3.7",
14-
"react": "^15.3.2",
15-
"react-dom": "^15.3.2"
16-
},
12+
"license": "ISC",
1713
"devDependencies": {
18-
"babel-core": "^6.18.2",
19-
"babel-loader": "^6.2.7",
20-
"babel-preset-es2015": "^6.18.0",
21-
"babel-preset-react": "^6.16.0",
22-
"css-loader": "^0.25.0",
23-
"file-loader": "^0.9.0",
24-
"html-webpack-plugin": "^2.24.1",
25-
"style-loader": "^0.13.1",
26-
"url-loader": "^0.5.7",
27-
"webpack": "^1.13.3",
28-
"webpack-dev-server": "^1.16.2"
14+
"babel-core": "^6.26.0",
15+
"babel-loader": "^7.1.4",
16+
"babel-preset-env": "^1.6.1",
17+
"babel-preset-react": "^6.24.1",
18+
"css-loader": "^0.28.11",
19+
"file-loader": "^1.1.11",
20+
"html-webpack-plugin": "^3.2.0",
21+
"mini-css-extract-plugin": "^0.4.0",
22+
"url-loader": "^1.0.1",
23+
"webpack": "^4.6.0",
24+
"webpack-cli": "^2.0.14",
25+
"webpack-dev-server": "3.1.0"
2926
},
30-
"repository": {
31-
"type": "git",
32-
"url": "https://github.com/Lemoncode/react-by-sample-es6"
27+
"dependencies": {
28+
"bootstrap": "^4.1.0",
29+
"react": "^16.3.2",
30+
"react-dom": "^16.3.2"
3331
}
3432
}

02 Properties/readme.md

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ In this sample we will introduce a basic React concept, handling properties.
44

55
We will add a _userName_ property and display it in the _Hello_ component.
66

7-
We will take as a starting point sample __01 Hello React__:
7+
We will take as a starting point sample __[01 Hello React](../01%20HelloReact/)__:
88

99
Summary steps:
1010

@@ -22,27 +22,72 @@ Install [Node.js and npm](https://nodejs.org/en/) if they are not already instal
2222

2323
- Copy the content from _01 HelloReact_ and execute `npm install`.
2424

25-
- Let's update __hello.jsx__ in order to reflect the new property added (_userName_) and display it using interpolation (`{props.userName}`):
25+
- Delete the __[main.js](./src/main.js)__ file. Our webpack configuration will only recognize one of the two and there may be conflict. Remember!!! There should never be two files with the same entry name.
2626

27-
```javascript
27+
- Let's update __[hello.jsx](./src/hello.jsx)__ in order to reflect the new property added (_userName_) and display it using interpolation (`{props.userName}`):
28+
29+
_[hello.jsx](./src/hello.jsx)_
30+
```diff
2831
import React from 'react';
2932

30-
export const HelloComponent = (props) => {
31-
return (
32-
<h2>Hello user: {props.userName}!</h2>
33-
);
34-
}
35-
```
33+
-- export const HelloComponent = () =>
34+
++ export const HelloComponent = (props) =>
35+
-- <p> Hello React!</p>
36+
++ <h2>Hello user: {props.userName}!</h2>
37+
```
38+
39+
- Let's update __[main.jsx](./src/main.jsx)__ and inform the _userName_ propery value:
40+
41+
_[main.jsx](./src/main.jsx)_
42+
```diff
43+
-- const personToGreet = "ES6";
44+
-- const messageToDisplay = `Hello ${personToGreet}!`;
45+
46+
-- document.write(messageToDisplay);
47+
48+
++ import React from 'react';
49+
++ import {render} from 'react-dom';
50+
++ import {HelloComponent} from './hello';
51+
52+
++ render(
53+
++ <HelloComponent userName="John" />
54+
++ , document.getElementById('root')
55+
++ );
56+
```
57+
58+
In this example we will only use the `render` method of React-DOM, so we will only import that (` import {render} from 'react-dom'; `).
59+
60+
Now we will modify the __[hello.jsx](./src/hello.jsx)__ to check that **properties** stick to a given contract (in this case prop `Username` is of type string and required)
3661

37-
- Let's update __main.jsx__ and inform the _userName_ propery value:
62+
(We can expand information here: [https://github.com/facebook/prop-types](https://github.com/facebook/prop-types))
3863

39-
```javascript
64+
_[hello.jsx](./src/hello.jsx)_
65+
```diff
4066
import React from 'react';
41-
import {render} from 'react-dom';
42-
import {HelloComponent} from './hello';
67+
++ import PropTypes from 'prop-types';
4368

44-
render(
45-
<HelloComponent userName="John" />
46-
, document.getElementById('root')
47-
);
69+
export const HelloComponent = (props) =>
70+
<h2>Hello user: {props.userName}!</h2>
71+
72+
++ HelloComponent.propTypes = {
73+
++ userName: PropTypes.string.isRequired
74+
++ };
75+
```
76+
77+
**Important**: Don't forget to import the component that manages it, `import PropTypes from 'prop-types';`.
78+
79+
- Execute the example:
80+
81+
```bash
82+
npm run build
4883
```
84+
85+
To generate the dist folder, and its contents, and
86+
87+
```bash
88+
npm start
89+
```
90+
91+
- Then, load http://localhost:8080/ in a browser to see the output.
92+
93+
![Browser Output](../99_readme_resources/01 HelloReact/browser_output.png "Browser Output")

02 Properties/readme_es.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 02 Propiedades
2+
3+
En esta demo, presentaremos un concepto básico de React, manejo de propiedades.
4+
5+
Agregaremos a la propiedad _userName_ y la mostraremos en el componente _Hello_.
6+
7+
Tomaremos como punto de partida la muestra __[01 Hello React](../01%20HelloReact/)__:
8+
9+
Pasos resumidos:
10+
11+
- Cree una propiedad en nuestro componente sin estado _Hello_ que contendrá el valor _userName_.
12+
13+
- Informarlo desde nuestro control padre.
14+
15+
16+
## Prerrequisitos
17+
18+
Instale [Node.js and npm](https://nodejs.org/en/) si aún no están instalados en su computadora.
19+
20+
> Verifique que esté ejecutando al menos los nodos v6.x.x y npm 3.x.x ejecutando `node -v` y` npm -v` en una ventana de terminal / consola. Las versiones anteriores pueden producir errores.
21+
22+
## Pasos para construirlo
23+
24+
- Copie el contenido de _01 HelloReact_ y ejecute `npm install`.
25+
26+
- Eliminar el archivo __[main.js](./src/main.js)__. Nuestra configuración de **webpack** solo reconocerá uno de los dos y puede haber un conflicto. ¡¡¡Recuerda!!! Nunca debe haber dos archivos con el mismo nombre de entrada.
27+
28+
- Actualicemos __[hello.jsx](./src/hello.jsx)__ para reflejar la nueva propiedad agregada (_userName_) y mostrarla mediante la interpolación (`{props.userName}`):
29+
30+
_[hello.jsx](./src/hello.jsx)_
31+
```diff
32+
import React from 'react';
33+
34+
-- export const HelloComponent = () =>
35+
++ export const HelloComponent = (props) =>
36+
-- <p> Hello React!</p>
37+
++ <h2>Hello user: {props.userName}!</h2>
38+
```
39+
40+
- Actualicemos __[main.jsx](./src/main.jsx)__ e informemos el valor de propiedad _userName_:
41+
42+
_[main.jsx](./src/main.jsx)_
43+
```diff
44+
-- const personToGreet = "ES6";
45+
-- const messageToDisplay = `Hello ${personToGreet}!`;
46+
47+
-- document.write(messageToDisplay);
48+
49+
++ import React from 'react';
50+
++ import {render} from 'react-dom';
51+
++ import {HelloComponent} from './hello';
52+
53+
++ render(
54+
++ <HelloComponent userName="John" />
55+
++ , document.getElementById('root')
56+
++ );
57+
```
58+
59+
En este ejemplo, solo usaremos el método `render` de React-DOM, por lo que solo lo importaremos (` import {render} from 'react-dom'; `).
60+
61+
Ahora modificaremos el __[hello.jsx](./src/hello.jsx)__ para verificar que **propiedades** se adhieren a un contrato determinado (en este caso, la propiedad `Username` es de tipo string y requerida)
62+
63+
(Podemos ampliar la información aquí: [https://github.com/facebook/prop-types](https://github.com/facebook/prop-types))
64+
65+
_[hello.jsx](./src/hello.jsx)_
66+
```diff
67+
import React from 'react';
68+
++ import PropTypes from 'prop-types';
69+
70+
export const HelloComponent = (props) =>
71+
<h2>Hello user: {props.userName}!</h2>
72+
73+
++ HelloComponent.propTypes = {
74+
++ userName: PropTypes.string.isRequired
75+
++ };
76+
```
77+
78+
**Importante**: no olvidar importar el componente que lo administra, `import PropTypes from 'prop-types';`.
79+
80+
81+
- Ejecuta el ejemplo:
82+
83+
```bash
84+
npm run build
85+
```
86+
87+
Para generar la carpeta dist, y su contenido, y
88+
89+
```bash
90+
npm start
91+
```
92+
93+
- Luego, carga http://localhost:8080/ en un navegador para ver la salida.
94+
95+

02 Properties/src/hello.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
23

3-
export const HelloComponent = (props) => {
4-
return (
5-
<h2>Hello user: {props.userName}!</h2>
6-
);
7-
}
4+
export const HelloComponent = (props) =>
5+
<h2>Hello user: {props.userName}!</h2>;
86

97
HelloComponent.propTypes = {
10-
userName: React.PropTypes.string.isRequired
11-
};
8+
userName: PropTypes.string.isRequired
9+
};
10+

02 Properties/src/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Sample 02 Properties</title>
5+
<title>React + ES6 by example</title>
66
</head>
77
<body>
88
<h1>Sample app</h1>
9-
<div id="root"></div>
9+
<div id="root">
10+
</div>
1011
</body>
1112
</html>

02 Properties/src/main.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22
import {render} from 'react-dom';
3-
import {HelloComponent} from './hello';
3+
import {HelloComponent} from './hello.jsx';
44

55
render(
6-
<HelloComponent userName="John" />
7-
, document.getElementById('root')
8-
);
6+
<HelloComponent userName="John" />,
7+
document.getElementById('root')
8+
);

0 commit comments

Comments
 (0)