-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-infosite-notes.js
More file actions
496 lines (369 loc) · 12.3 KB
/
react-infosite-notes.js
File metadata and controls
496 lines (369 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* composability */
// practice in composability
// creating components
function MainContent() {
return(
<h1>I'm learning React!</h1>
)
}
ReactDOM.render(
<div>
<MainContent />
</div>
//document.getElementById("root")
)
/* declarativity */
// declarative = tell program "what should be done"
// imperative = tell program "how it should be done"
// vanilla JS
// ReactDOM.render(<h1>Hello, React!</h1>, document.getElementById("root"))
/*
Challenge - recreate the above line of code in vanilla JS by creating and
appending an h1 to our div#root (without using innerHTML).
- Create a new h1 element
- Give it some textContent
- Give it a class name of "header"
- append it as a child of the div#root
*/
const h1 = document.createElement("h1")
h1.textContent = "Hello, React!"
h1.className = "header"
document.getElementById("root").append(h1)
// this gets old quickly
// we get to write js as if it were html
// relying on react on how to turn declarative text int js code to append to the DOM
// 4 lines of code vs 1 line
/* JSX */
// JSX == javascript XML. flavor of JS that looks like html
// makes it declarative
const element = <h1 className="header">this is JSX</h1>
console.log(element)
// jsx makes js objects that describe dom elements that react puts on the page
ReactDOM.render(element, document.getElementById("root"))
// only return a single parent element w jsx
// no sibling elements
// wrap multiple children elements in a parent element
const page = (
<div>
<h1 className="header">This is JSX</h1>
<p>This is a paragraph</p>
</div>
)
// getting back a js object
console.log(page)
ReactDOM.render(
page, // JSX being saved under page. place page and react will render
document.getElementById("root")
)
/*
Challenge:
Create a navbar in JSX:
- Use the semantic `nav` element as the parent wrapper
- Have an h1 element with the brand name of your "website"
- Insert an unordered list for the other nav elements
- Inside the `ul`, have three `li`s for "Pricing",
"About", and "Contact"
- Don't worry about styling yet - it'll just be plain-looking HTML for now
*/
const navBar = (
<nav>
<h1>Website</h1>
<ul>
<li>Pricing</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
)
ReactDOM.render(
navBar,
document.getElementById("root")
)
// dependencies = package.json
import React from "react"
//when using reactDOM
import ReactDOM from "react-dom"
/* .append() vs ReactDOM.render() */
/**
Challenge: find out what happens if we try to append JSX
to our div#root using .append() instead of ReactDOM
1. Create a sample page in JSX (≥ 4 elements) and save them in a variable
2. Select the div with the ID of "root" and use `.append()` to append
your JSX
3. See if you can guess what will show up in the browser before running
the code
4. See if you can explain what actually shows up in the browser
*/
const page = (
<div>
<h1>Hello, React!</h1>
<ol>
<li>Here's a list!</li>
</ol>
</div>
)
document.getElementById("root").append(page)
// string represenation of a js object
// stringify using json.
// puts json on the page and we get a java script object
// jsx only returns plain js objects and is not recognized by the browser.
// only when we render it, react turns it into dom elements that the browser can interpret.
import ReactDOM from "react-dom"
ReactDOM.render(page, document.getElementById("root"))
// changing so it renders on the page
/* project part 1 - markup */
/*
Challenge: Starting from scratch, build and render the
HTML for our section project. Check the Google slide for
what you're trying to build.
We'll be adding styling to it later.
Hints:
* The React logo is a file in the project tree, so you can
access it by using `src="./react-logo.png" in your image
element
* You can also set the `width` attribute of the image element
just like in HTML. In the slide, I have it set to 40px
*/
import React from "react"
import ReactDOM from "react-dom"
const page = (
<div>
<img src="react-logo.png" alt="none" width ="40"></img>
<h1>Fun facts about React</h1>
<ul>
<li>Was first released in 2013</li>
<li>Was originally created by Jordan Walker</li>
<li>Has well over 100K stars on Github</li>
<li>Is maintained by Facebook</li>
<li>Powers thousands of enterprises, including mobile apps</li>
</ul>
</div>
)
ReactDOM.render(
page,
document.getElementById("root")
)
/* pop quiz */
/*1. Why do we need to `import React from "react"` in our files?
if you want to use JSX, you need to import for the compiler to understand JSX objects.
ANS: jsx syntax is defined in react.
2. If I were to console.log(page) in index.js, what would show up?
The JS object string representations.
ANS: A JS object.
3. What's wrong with this code:
```
const page = (
<h1>Hello</h1>
<p>This is my website!</p>
)
```
You are not wrapping it within a parent element. It is not possible to have two sibling elements.
ANS: it needs to be nested under a single parent element.
4. What does it mean for something to be "declarative" instead of "imperative"?
Declarative is telling the compiler how something is done, while imperative is telling the compiler what to do. Vanilla JS is imperative, React is declarative.
React takes the declared code and performs all of the imperative steps (JS/DOM) to render onto the page.
ANS: declarative tells what to do, imperative tells how to do.
5. What does it mean for something to be "composable"?
You can build components to reuse them in order sections of your project.
ANS: create small pieces to make something larger/greater than the individual peices.
*/
/* custom components */
// saving jsx elements in a variable & rendering via react
// doesn't allow flexibility to create components in react
// function == allow code to execute just by calling the function
// react function == create user interface by calling functions
// use pascal case = capitalize first letter
// wrap function call in angle brackets:
function TemporaryName() {
return()
}
<TemporaryName />
/**
Challenge:
Part 1: Create a page of your own using a custom Page component
It should return an ordered list with the reasons why you're
excited to be learning React :)
Render your list to the page
*/
import React from "react"
import ReactDOM from "react-dom"
function PageComponent() {
return (
<ol>
<li> So I can succeed in the internship</li>
<li> So I can put it on my resume</li>
<li> So I understand how front-end works</li>
</ol>
)
}
ReactDOM.render(<PageComponent />, document.getElementById("root"));
/* custom components part 2 */
/**
Challenge:
Part 2:
- Add a `header` element with a nested `nav` element. Inside the `nav`,
include a `img` element with the image of the React logo inside
(src="./react-logo.png") and make sure to set the width to something
more manageable so it doesn't take up the whole screen
- Add an `h1` with some text describing the page. (E.g. "Reasons
I'm excited to learn React"). Place it above the ordered list.
- Add a `footer` after the list that says:
"© 20xx <last name here> development. All rights reserved."
*/
import React from "react"
import ReactDOM from "react-dom"
function Page() {
return (
<div>
<header>
<nav>
<img src="./react-logo.png" alt="none" width="40"></img>
</nav>
</header>
<h1>Reasons I'm excited to learn React</h1>
<ol>
<li>It's a popular library, so I'll be
able to fit in with the cool kids!</li>
<li>I'm more likely to get a job as a developer
if I know React</li>
</ol>
<footer>© 20xx Meshorer development. All rights reserved.</footer>
</div>
)
}
ReactDOM.render(<Page />, document.getElementById("root"))
/* custom components quiz */
/* Quiz!
1. What is a React component?
an independent and reusable bit of code. serve the same purpose as JS functions but work in isolation and return jsx (js objects) that turn into real dom eleemnts
2. What's wrong with this code?
```
function myComponent() {
return (
<small>I'm tiny text!</small>
)
}
```
not using pascal case
3. What's wrong with this code?
```
function Header() {
return (
<header>
<nav>
<img src="./react-logo.png" width="40px" />
</nav>
</header>
)
}
ReactDOM.render(Header(), document.getElementById("root"))
```
not calling the function correctly
should be <Header />
surround with angle brackets as if it were an html tag
create an instance of the component
*/
/* parent/child components */
/**
Challenge:
- Move the `footer` into its own component called "Footer"
and render that component inside the Page component.
- Move the `h1` and `ol` together into another component
called "MainContent" and render inside Page as well.
*/
function Header() {
return (
<header>
<nav>
<img src="./react-logo.png" width="40px" />
</nav>
</header>
)
}
function Content() {
return(
<div>
<h1>Reasons I'm excited to learn React</h1>
<ol>
<li>It's a popular library, so I'll be
able to fit in with the cool kids!</li>
<li>I'm more likely to get a job as a developer
if I know React</li>
</ol>
</div>
)
}
function Footer() {
return(
<footer>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
function Page() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
)
}
ReactDOM.render(<Page />, document.getElementById("root"))
/* styling with classes */
/**
Challenge:
- Add an `ul` inside the Header's `nav` and create
the following `li`s: "Pricing", "About", & "Contact"
*/
function Header() {
return (
<header>
<nav>
<img src="./react-logo.png" width="40px" />
<ul>
<li>Pricing</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</header>
)
}
// className
// react is turning JSX elements into native DOM elements
// ul.className = xxx in vanilla js
import Header from "./my-app/src/components/Header"
// ./ is a file in my own project director
// not a package in dependencies
import Footer from "./my-app/src/components/Footer"
import MainContent from "./my-app/src/components/MainContent"
/* quick mental outline */
// in order to move navbar stuff all the way to the right (sticky to the right) set auto margin: 0.
// create container div
// surrounds whole page
// main two sections
// nav bar, main content
// in nav, image and two title tags
// in main, title and list items
// next challenge, did in scrimba. can be visible via the my-app folder
/**
Challenge: Project setup
- Create a `components` folder
- Create the following components in separate files inside
the components folder. In each one, just render an `h1`
with the name of the component (e.g. return <h1>Navbar goes here</h1>):
- Navbar
- Main
- Create an App component outside the components folder (sibling to
the index.js file)
- Have App render the Navbar and Main components
- Import and render the App component inside of index.js using ReactDOM
- At this point you should have your "Navbar goes here" etc. showing up
in the mini-browser.
- Go to Google fonts and get the "Inter" font with weights 400, 600, and 700.
Put the links to those fonts ABOVE the style.css link in index.html (Use
the `<link/>` elements instead of the @import or npm options for getting
the fonts. You may need to do some extra research to figure out how this
works if you haven't done it before)
*/