|
5 | 5 | 👨💼 Let's convert this to use React! But don't worry, we won't be doing any JSX just
|
6 | 6 | yet... You're going to use raw React APIs here.
|
7 | 7 |
|
8 |
| -In modern applications you'll get React and React DOM files from a "package |
9 |
| -registry" like [npm](https://npmjs.com) ([react](https://npm.im/react) and |
| 8 | +In modern applications, you typically get React and React DOM files from a "package registry", |
| 9 | +like [npm](https://npmjs.com) where packages we use are ([react](https://npm.im/react) and |
10 | 10 | [react-dom](https://npm.im/react-dom)).
|
11 | 11 |
|
12 |
| -To keep things as simple as possible in these first exercises, we'll be |
13 |
| -importing the file `public/react.js` at `/react.js` and |
14 |
| -`public/react-dom/client.js` at `/react-dom/client.js`. Those files re-export |
15 |
| -the packages from [esm.sh](https://esm.sh). |
| 12 | +<callout-info class="aside"> |
| 13 | + A package registry is a centralized repository where developers can publish |
| 14 | + and share reusable code packages. |
| 15 | +</callout-info> |
| 16 | + |
| 17 | +To keep things simple for these exercises, we'll import React from slightly different locations: |
| 18 | + |
| 19 | +- `public/react.js` (accessed at `/react.js`) |
| 20 | +- `public/react-dom/client.js` (accessed at `/react-dom/client.js`) |
| 21 | + |
| 22 | +These files use a service called [esm.sh](https://esm.sh) to provide the React packages. |
| 23 | + |
| 24 | +Don't worry too much about [esm.sh](https://esm.sh) for now - it's just helping us load React easily in these exercises. |
16 | 25 |
|
17 |
| -Here's a simple example of the API: |
| 26 | +Here's a simple example of how to use the React createElement API: |
18 | 27 |
|
19 | 28 | ```typescript
|
20 | 29 | import { createElement } from '/react.js'
|
@@ -42,34 +51,43 @@ import { createRoot } from 'react-dom/client'
|
42 | 51 |
|
43 | 52 | </callout-info>
|
44 | 53 |
|
45 |
| -The "props" in `elementProps` above is short for "properties" and they're a |
46 |
| -foundational part of React elements. You can think of the element type as a |
47 |
| -blueprint for the kind of React component to create and the props are the inputs |
48 |
| -for when React actually creates that element. |
| 54 | +The "props" in `elementProps` above is short for "properties". Props are a key concept |
| 55 | +in React - they're the way we pass data into our elements. You can think of the element |
| 56 | +type as a blueprint for the kind of React component to create, and the props are the |
| 57 | +inputs that customize that element. |
49 | 58 |
|
50 |
| -`children` is a special prop. You can pass a single element like above, or an |
51 |
| -array of children (if there's more than one). You can also pass the children as |
52 |
| -any number of additional arguments: |
| 59 | +`children` is a special prop in React. It represents the content inside an element. |
| 60 | +You have a few ways to specify children: |
| 61 | + |
| 62 | +1. As a prop (like in the first example above) |
| 63 | +2. As multiple arguments to `createElement` |
| 64 | +3. As an array |
| 65 | + |
| 66 | +Here's an example showing the last two methods: |
53 | 67 |
|
54 | 68 | ```typescript
|
55 | 69 | const elementProps = { id: 'element-id' }
|
56 |
| -const child1 = 'Hello' |
57 |
| -const child2 = ' ' |
58 |
| -const child3 = 'world!' |
59 | 70 | const elementType = 'h1'
|
60 |
| -const reactElement = createElement( |
| 71 | + |
| 72 | +// Method 2: Multiple arguments |
| 73 | +const reactElement1 = createElement( |
61 | 74 | elementType,
|
62 | 75 | elementProps,
|
63 |
| - child1, |
64 |
| - child2, |
65 |
| - child3, |
| 76 | + 'Hello', |
| 77 | + ' ', |
| 78 | + 'world!', |
66 | 79 | )
|
67 |
| -createRoot(rootElement).render(reactElement) |
| 80 | + |
| 81 | +// Method 3: Array of children |
| 82 | +const children = ['Hello', ' ', 'world!'] |
| 83 | +const reactElement2 = createElement(elementType, elementProps, children) |
| 84 | + |
| 85 | +createRoot(rootElement).render(reactElement1) // or reactElement2 |
68 | 86 | ```
|
69 | 87 |
|
70 |
| -Alright! Let's do this! |
| 88 | +Alright! Let's put this into practice! |
71 | 89 |
|
72 | 90 | <callout-info class="aside">
|
73 |
| - 💰 Tip: `console.log` the `reactElement` to see what it looks like. You might |
74 |
| - find it kinda interesting! |
| 91 | + 💰 Tip: Use `console.log(reactElement)` to see what your created element looks |
| 92 | + like. You might find the structure interesting! |
75 | 93 | </callout-info>
|
0 commit comments