Skip to content

Commit 180af92

Browse files
committed
sync
1 parent 30ac3de commit 180af92

File tree

1 file changed

+43
-25
lines changed
  • exercises/02.raw-react/01.problem.elements

1 file changed

+43
-25
lines changed

exercises/02.raw-react/01.problem.elements/README.mdx

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
👨‍💼 Let's convert this to use React! But don't worry, we won't be doing any JSX just
66
yet... You're going to use raw React APIs here.
77

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
1010
[react-dom](https://npm.im/react-dom)).
1111

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.
1625

17-
Here's a simple example of the API:
26+
Here's a simple example of how to use the React createElement API:
1827

1928
```typescript
2029
import { createElement } from '/react.js'
@@ -42,34 +51,43 @@ import { createRoot } from 'react-dom/client'
4251

4352
</callout-info>
4453

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.
4958

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:
5367

5468
```typescript
5569
const elementProps = { id: 'element-id' }
56-
const child1 = 'Hello'
57-
const child2 = ' '
58-
const child3 = 'world!'
5970
const elementType = 'h1'
60-
const reactElement = createElement(
71+
72+
// Method 2: Multiple arguments
73+
const reactElement1 = createElement(
6174
elementType,
6275
elementProps,
63-
child1,
64-
child2,
65-
child3,
76+
'Hello',
77+
' ',
78+
'world!',
6679
)
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
6886
```
6987

70-
Alright! Let's do this!
88+
Alright! Let's put this into practice!
7189

7290
<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!
7593
</callout-info>

0 commit comments

Comments
 (0)