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: README.md
+21-6Lines changed: 21 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,17 @@
4
4
5
5
— Facebook (the creators/maintainers of this thing)
6
6
7
+
**Table of Contents**
8
+
9
+
-[What is React?](#what-is-react)
10
+
-[Components](#components)
11
+
-[JSX](#jsx)
12
+
-[Props](#props)
13
+
-[State](#state)
14
+
-[Rendering](#rendering)
15
+
-[Create React App](#create-react-app)
16
+
-[Effects](#effects)
17
+
7
18
---
8
19
9
20
## Assumptions
@@ -103,7 +114,7 @@ function Clock() {
103
114
}
104
115
```
105
116
106
-
(See [`clock.html`](https://davidhartsough.com/intro-to-react/examples/clock.html) and [source](https://github.com/davidhartsough/intro-to-react/examples/clock.html).)
117
+
(See [`clock.html`](https://davidhartsough.com/intro-to-react/examples/clock.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/clock.html).)
107
118
108
119
JSX allows you to create components that mix your logic and data with your markup — tastefully. (By that I mean: better than template languages.)
109
120
@@ -125,7 +136,7 @@ function App() {
125
136
}
126
137
```
127
138
128
-
(See [`random.html`](https://davidhartsough.com/intro-to-react/examples/random.html) and [source](https://github.com/davidhartsough/intro-to-react/examples/random.html).)
139
+
(See [`random.html`](https://davidhartsough.com/intro-to-react/examples/random.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/random.html).)
129
140
130
141
This demonstrates another beauty of JSX: components become custom markup "elements", which are always denoted with capital letters. (Every component _must_ begin with a capital letter; otherwise React treats anything starting with lowercase letters as HTML DOM tags.) In this case, the RandomPhoto component is reused thrice and rendered inside the App component's element tree.
131
142
@@ -146,7 +157,7 @@ function ColorList() {
146
157
}
147
158
```
148
159
149
-
(See [`color-list.html`](https://davidhartsough.com/intro-to-react/examples/color-list.html) and [source](https://github.com/davidhartsough/intro-to-react/examples/color-list.html).)
160
+
(See [`color-list.html`](https://davidhartsough.com/intro-to-react/examples/color-list.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/color-list.html).)
150
161
151
162
Here we use a JavaScript array ofstrings (in the `colors` variable) to create 3`<li>` elements and stylize the font of each with its respective color.
152
163
@@ -191,7 +202,7 @@ function App() {
191
202
}
192
203
```
193
204
194
-
(See [`color-lists.html`](https://davidhartsough.com/intro-to-react/examples/color-lists.html) and [source](https://github.com/davidhartsough/intro-to-react/examples/color-lists.html).)
205
+
(See [`color-lists.html`](https://davidhartsough.com/intro-to-react/examples/color-lists.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/color-lists.html).)
195
206
196
207
Every component is a function. Every component function recieves one parameter called props. Every props argument is an object. Every key-value pair is an individual prop. Every prop is passed to a component via HTML-attribute-like syntax in JSX.
197
208
@@ -223,7 +234,7 @@ function App() {
223
234
}
224
235
```
225
236
226
-
(See [`greeting.html`](https://davidhartsough.com/intro-to-react/examples/greeting.html) and [source](https://github.com/davidhartsough/intro-to-react/examples/greeting.html).)
237
+
(See [`greeting.html`](https://davidhartsough.com/intro-to-react/examples/greeting.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/greeting.html).)
227
238
228
239
This demonstrates the use of default props and "children"props. To set defaults for a component's props, all you have to do is set defaults for the function parameter.
229
240
@@ -248,6 +259,8 @@ function Clicker() {
248
259
}
249
260
```
250
261
262
+
(See [`clicker.html`](https://davidhartsough.com/intro-to-react/examples/clicker.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/clicker.html).)
263
+
251
264
### Syntax
252
265
253
266
State is optional, so to declaratively include state, you must invoke `useState`, which you import from React. `useState` accepts one parameter, which sets the default for the state variable, and then it returns a pair (array) of values: `[1]` the current state variable and `[2]` a function that updates it. Best practices encourage you to destructure this returned array into two separate variables that should be named as a pair. The first returned value should be the name of the local variable itself (ex: `age` or `color`), but the second returned value should be named the same but with "set" as a prefix and then camelCase following (ex: `setAge` or `setColor`).
@@ -270,6 +283,8 @@ function NameForm() {
270
283
}
271
284
```
272
285
286
+
(See [`name-form.html`](https://davidhartsough.com/intro-to-react/examples/name-form.html) and [source](https://github.com/davidhartsough/intro-to-react/blob/master/examples/name-form.html).)
287
+
273
288
## Rendering
274
289
275
290
So it's about dang time I showed you how to actually render any of these components you've been making. And the good news is that it's just a dead simple one-liner call to `ReactDom.render()`.
@@ -285,7 +300,7 @@ Yep. That's it.
285
300
286
301
## Create React App
287
302
288
-
So far we've only tried things out in an HTML file with CDN imports of development JS files and then inline Babel scripts... That's not the real deal. It's just for testing in a tiny sandbox. If you actually want to create a legit, production-ready React app, use **Create React App**. See links to the GitHub,
303
+
So far we've only tried things out in an HTML file with CDN imports of development JS files and then inline Babel scripts... That's not the real deal. It's just for testing in a tiny sandbox. If you actually want to create a legit, production-ready React app, use [**Create React App**](https://create-react-app.dev/) ([GitHub](https://github.com/facebook/create-react-app)).
289
304
290
305
Wanna try it? If you have Node >= 8.10 and npm >= 5.6 on your machine, then just run these commands in your terminal:
0 commit comments