You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-frameworks/01-react/01-previous/01-concepts/readme.md
+34-50Lines changed: 34 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,33 +80,14 @@ Now the content is generated by JavaScript. We can confirm that it’s the JavaS
80
80
81
81
### Components
82
82
83
-
Let’s start breaking our list into parts. First, we separate the title:
83
+
Let’s start breaking our list into parts.We separate the title and the list:
84
84
85
-
```diff
86
-
import "./styles.css";
85
+
````diff
86
+
import "./styles.css";
87
87
88
88
+ const Header = () => {
89
-
+ return `<h4>Lista de usuarios</h4>`;
89
+
+ return `<h4>Lista de usuarios</h4>`;
90
90
+ };
91
-
+
92
-
document.getElementById("app").innerHTML = `
93
-
- <h4>Lista de usuarios</h4>
94
-
+ ${Header()}
95
-
<div>1955: Rick Sánchez</div>
96
-
<div>8765: Beth Harmon</div>
97
-
<div>7562: Farrokh Bulsara</div>
98
-
```
99
-
100
-
This function we just created, in React, is a component. That is, **in React, components are functions.** For now, this component returns a piece of our application, in this case the title, which renders something in the DOM.
101
-
102
-
Let’s continue splitting or componentizing our application. We’ll create a new component that returns just the list..
103
-
104
-
```diff
105
-
import "./styles.css";
106
-
107
-
const Header = () => {
108
-
return `<h4>Lista de usuarios</h4>`;
109
-
};
110
91
111
92
+ const List = () => {
112
93
+ return `
@@ -118,14 +99,17 @@ const Header = () => {
118
99
+ };
119
100
120
101
document.getElementById("app").innerHTML = `
121
-
${Header()}
122
-
- <div>1955: Rick Sánchez</div>
123
-
- <div>8765: Beth Harmon</div>
124
-
- <div>7562: Farrokh Bulsara</div>
125
-
+ ${List()}
102
+
+ ${Header()}
103
+
+ ${List()}
104
+
- <h4>Lista de usuarios</h4>
105
+
- <div>1955: Rick Sánchez</div>
106
+
- <div>8765: Beth Harmon</div>
107
+
- <div>7562: Farrokh Bulsara</div>
126
108
`;
127
109
```
128
110
111
+
This functions we just created, in React, are components. That is, **in React, components are functions.** For now, these components return a piece of our application, in this case the title and the list, which render something in the DOM.
112
+
129
113
### Props
130
114
131
115
Now let’s create a component that renders each user in the DOM. To do this, we’ll create a component (function) that receives a user object with an `id` and `name`:
@@ -144,20 +128,20 @@ const Header = () => {
144
128
const List = () => {
145
129
return `
146
130
<div>
147
-
+${User({id: 1955, name 'Rick Sánchez'})}
148
-
+${User({id: 8765, name 'Beth Harmon'})}
149
-
+${User({id: 7562, name 'Farrokh Bulsara'})}
150
-
-<div>1955: Rick Sánchez</div>
151
-
-<div>8765: Beth Harmon</div>
152
-
-<div>7562: Farrokh Bulsara</div>
131
+
+ ${User({id: 1955, name 'Rick Sánchez'})}
132
+
+ ${User({id: 8765, name 'Beth Harmon'})}
133
+
+ ${User({id: 7562, name 'Farrokh Bulsara'})}
134
+
- <div>1955: Rick Sánchez</div>
135
+
- <div>8765: Beth Harmon</div>
136
+
- <div>7562: Farrokh Bulsara</div>
153
137
</div>`;
154
138
};
155
139
156
140
document.getElementById("app").innerHTML = `
157
141
${Header()}
158
142
${List()}
159
143
`;
160
-
```
144
+
````
161
145
162
146
In React jargon, the input arguments we pass to components are known as `props`. A bit later we’ll see that in React, the syntax for running a component is very different from this. However, it’s very important to keep in mind that even though the syntax is different, in the end what we’re doing is invoking functions and passing them input arguments.
163
147
@@ -189,10 +173,10 @@ const List = () => {
189
173
+ const users = getUsers();
190
174
return `
191
175
<div>
192
-
+${users.map(user=>User(user)).join('')}
193
-
-${User({id: 1955, name 'Rick Sánchez'})}
194
-
-${User({id: 8765, name 'Beth Harmon'})}
195
-
-${User({id: 7562, name 'Farrokh Bulsara'})}
176
+
+ ${users.map(user=>User(user)).join('')}
177
+
- ${User({id: 1955, name 'Rick Sánchez'})}
178
+
- ${User({id: 8765, name 'Beth Harmon'})}
179
+
- ${User({id: 7562, name 'Farrokh Bulsara'})}
196
180
</div>`;
197
181
};
198
182
@@ -222,8 +206,8 @@ const List = () => {
222
206
const users = getUsers();
223
207
return `
224
208
<div>
225
-
-${users.map((user) => User(user)).join("")}
226
-
+${users.map((user) => User({ user })).join("")}
209
+
- ${users.map((user) => User(user)).join("")}
210
+
+ ${users.map((user) => User({ user })).join("")}
227
211
</div>`;
228
212
};
229
213
@@ -249,12 +233,12 @@ If we update it with a setTimeout, we see the value changes in the console, but
Copy file name to clipboardExpand all lines: 04-frameworks/01-react/01-previous/01-concepts/readme_es.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ Ahora el contenido lo genera JavaScript. Comprobamos que es el fichero de JavaSc
80
80
81
81
### Componentes
82
82
83
-
Vamos a ir partiendo nuestra lista en partes. Así, vamos a segregar el header y la lista en dos componentes a parte
83
+
Vamos a ir partiendo nuestra lista en partes. Así, vamos a segregar el header y la lista en dos componentes a parte.
84
84
85
85
```diff
86
86
import "./styles.css";
@@ -99,16 +99,16 @@ import "./styles.css";
99
99
+ };
100
100
101
101
document.getElementById("app").innerHTML = `
102
-
+${Header()}
103
-
+${List()}
104
-
-<h4>Lista de usuarios</h4>
105
-
-<div>1955: Rick Sánchez</div>
106
-
-<div>8765: Beth Harmon</div>
107
-
-<div>7562: Farrokh Bulsara</div>
102
+
+ ${Header()}
103
+
+ ${List()}
104
+
- <h4>Lista de usuarios</h4>
105
+
- <div>1955: Rick Sánchez</div>
106
+
- <div>8765: Beth Harmon</div>
107
+
- <div>7562: Farrokh Bulsara</div>
108
108
`;
109
109
```
110
110
111
-
A esta funciones que acabamos de crear, en React, las vamos a llamar componentes. Es decir, **en React los componentes son funciones.** Por el momento estos componentes nos devuelven un trocito de nuestra aplicación, en este caso el título y la lista, es decir, renderizan algo en el DOM.
111
+
A estas funciones que acabamos de crear, en React, las vamos a llamar componentes. Es decir, **en React los componentes son funciones.** Por el momento estos componentes nos devuelven un trocito de nuestra aplicación, en este caso el título y la lista, es decir, renderizan algo en el DOM.
112
112
113
113
### Props
114
114
@@ -128,12 +128,12 @@ const Header = () => {
128
128
const List = () => {
129
129
return `
130
130
<div>
131
-
+${User({id: 1955, name 'Rick Sánchez'})}
132
-
+${User({id: 8765, name 'Beth Harmon'})}
133
-
+${User({id: 7562, name 'Farrokh Bulsara'})}
134
-
-<div>1955: Rick Sánchez</div>
135
-
-<div>8765: Beth Harmon</div>
136
-
-<div>7562: Farrokh Bulsara</div>
131
+
+ ${User({id: 1955, name 'Rick Sánchez'})}
132
+
+ ${User({id: 8765, name 'Beth Harmon'})}
133
+
+ ${User({id: 7562, name 'Farrokh Bulsara'})}
134
+
- <div>1955: Rick Sánchez</div>
135
+
- <div>8765: Beth Harmon</div>
136
+
- <div>7562: Farrokh Bulsara</div>
137
137
</div>`;
138
138
};
139
139
@@ -173,10 +173,10 @@ const List = () => {
173
173
+ const users = getUsers();
174
174
return `
175
175
<div>
176
-
+${users.map(user=>User(user)).join('')}
177
-
-${User({id: 1955, name 'Rick Sánchez'})}
178
-
-${User({id: 8765, name 'Beth Harmon'})}
179
-
-${User({id: 7562, name 'Farrokh Bulsara'})}
176
+
+ ${users.map(user=>User(user)).join('')}
177
+
- ${User({id: 1955, name 'Rick Sánchez'})}
178
+
- ${User({id: 8765, name 'Beth Harmon'})}
179
+
- ${User({id: 7562, name 'Farrokh Bulsara'})}
180
180
</div>`;
181
181
};
182
182
@@ -206,8 +206,8 @@ const List = () => {
206
206
const users = getUsers();
207
207
return `
208
208
<div>
209
-
-${users.map((user) => User(user)).join("")}
210
-
+${users.map((user) => User({ user })).join("")}
209
+
- ${users.map((user) => User(user)).join("")}
210
+
+ ${users.map((user) => User({ user })).join("")}
211
211
</div>`;
212
212
};
213
213
@@ -233,12 +233,12 @@ Si lo actualizamos con un setTimeout, vemos que el valor cambia en consola, pero
0 commit comments