Skip to content

Commit 580eac4

Browse files
authored
update day-13 code snippets
1 parent a258f56 commit 580eac4

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

day-13/post.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ We've already seen this before where we've iterated over a list of objects and r
2525
Since JSX is seen as plain JavaScript by the browser, we can use any ole' JavaScript inside the template tags in JSX. We've already seen this in action. As a quick demo:
2626

2727
```javascript
28+
const a = 10;
29+
const ShowA = () => <div>{a}</div>;
30+
const MultipleA = () => <div>{a * a}</div>;
31+
2832
const App = (props) => {
2933
return (
30-
<ul>
31-
{a.map(i => {
32-
return <li>{i}</li>
33-
})}
34-
</ul>
34+
<div className="app">
35+
<ShowA />
36+
<MultipleA />
37+
</div>
3538
)
3639
}
3740
```
@@ -49,15 +52,16 @@ const a = [1, 10, 100, 1000, 10000];
4952
We can map over the `a` variable here inside our components and return a list of React components that will build the virtual DOM for us.
5053

5154
```javascript
52-
const App = (props) => {
55+
const a = [1, 10, 100, 1000, 10000];
56+
const Repeater = () => {
5357
return (
5458
<ul>
5559
{a.map(i => {
56-
return <li>{i}</li>
60+
return <li>{i}</li>;
5761
})}
5862
</ul>
59-
)
60-
}
63+
);
64+
};
6165
```
6266

6367
> What is the `map()` function?
@@ -143,8 +147,8 @@ const Formatter = (props) => {
143147
We can use the `React.Children` object to map over a list of React objects and let React do this heavy-lifting. The result of this is a much cleaner `Formatter` component (not perfect, but functional):
144148

145149
```javascript
146-
const Formatter = ({format, state}) => {
147-
let children = format.split('').map(e => {
150+
const Formatter = (props) => {
151+
let children = props.format.split('').map(e => {
148152
if (e == 'h') {
149153
return <Hour />
150154
} else if (e == 'm') {
@@ -161,7 +165,7 @@ const Formatter = ({format, state}) => {
161165
});
162166
return (<span>
163167
{React.Children
164-
.map(children, c => React.cloneElement(c, state))}
168+
.map(children, c => React.cloneElement(c, props))}
165169
</span>)
166170
}
167171
```
@@ -186,7 +190,7 @@ const Formatter = ({format, state}) => {
186190
> 2. Any `props` we want to add to the instance
187191
> 3. Any `children` we want it to have.
188192
>
189-
> In our `Formatter` example, we're creating a copy of all the children in the list (the `<Hour />`, `<Minute />`, etc. components) and passing them the `state` object as their props.
193+
> In our `Formatter` example, we're creating a copy of all the children in the list (the `<Hour />`, `<Minute />`, etc. components) and passing them the `props` object as their props.
190194
191195
The `React.Children` object provides some nice utility functions for dealing with children. Our `Formatter` example above uses the `map` function to iterate through the children and clone each one in the list. It creates a `key` (if necessary) for each one, freeing us from having to manage the uniqueness ourselves.
192196

0 commit comments

Comments
 (0)