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: src/content/learn/conditional-rendering.md
+16-16
Original file line number
Diff line number
Diff line change
@@ -52,13 +52,13 @@ export default function PackingList() {
52
52
53
53
</Sandpack>
54
54
55
-
Notice that some of the `Item` components have their `isPacked` prop set to `true` instead of `false`. You want to add a checkmark (✔) to packed items if `isPacked={true}`.
55
+
Notice that some of the `Item` components have their `isPacked` prop set to `true` instead of `false`. You want to add a checkmark (✅) to packed items if `isPacked={true}`.
56
56
57
57
You can write this as an [`if`/`else` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) like so:
58
58
59
59
```js
60
60
if (isPacked) {
61
-
return <li className="item">{name} ✔</li>;
61
+
return <li className="item">{name} ✅</li>;
62
62
}
63
63
return <li className="item">{name}</li>;
64
64
```
@@ -70,7 +70,7 @@ If the `isPacked` prop is `true`, this code **returns a different JSX tree.** Wi
70
70
```js
71
71
function Item({ name, isPacked }) {
72
72
if (isPacked) {
73
-
return <li className="item">{name} ✔</li>;
73
+
return <li className="item">{name} ✅</li>;
74
74
}
75
75
return <li className="item">{name}</li>;
76
76
}
@@ -159,7 +159,7 @@ In practice, returning `null` from a component isn't common because it might sur
159
159
In the previous example, you controlled which (if any!) JSX tree would be returned by the component. You may already have noticed some duplication in the render output:
160
160
161
161
```js
162
-
<li className="item">{name} ✔</li>
162
+
<li className="item">{name} ✅</li>
163
163
```
164
164
165
165
is very similar to
@@ -172,7 +172,7 @@ Both of the conditional branches return `<li className="item">...</li>`:
172
172
173
173
```js
174
174
if (isPacked) {
175
-
return <li className="item">{name} ✔</li>;
175
+
return <li className="item">{name} ✅</li>;
176
176
}
177
177
return <li className="item">{name}</li>;
178
178
```
@@ -187,7 +187,7 @@ Instead of this:
187
187
188
188
```js
189
189
if (isPacked) {
190
-
return <li className="item">{name} ✔</li>;
190
+
return <li className="item">{name} ✅</li>;
191
191
}
192
192
return <li className="item">{name}</li>;
193
193
```
@@ -197,12 +197,12 @@ You can write this:
197
197
```js
198
198
return (
199
199
<li className="item">
200
-
{isPacked ? name + ' ✔' : name}
200
+
{isPacked ? name + ' ✅' : name}
201
201
</li>
202
202
);
203
203
```
204
204
205
-
You can read it as *"if `isPacked` is true, then (`?`) render `name + ' ✔'`, otherwise (`:`) render `name`"*.
205
+
You can read it as *"if `isPacked` is true, then (`?`) render `name + ' ✅'`, otherwise (`:`) render `name`"*.
206
206
207
207
<DeepDive>
208
208
@@ -222,7 +222,7 @@ function Item({ name, isPacked }) {
222
222
<li className="item">
223
223
{isPacked ? (
224
224
<del>
225
-
{name + ' ✔'}
225
+
{name + ' ✅'}
226
226
</del>
227
227
) : (
228
228
name
@@ -265,7 +265,7 @@ Another common shortcut you'll encounter is the [JavaScript logical AND (`&&`) o
265
265
```js
266
266
return (
267
267
<li className="item">
268
-
{name} {isPacked && '✔'}
268
+
{name} {isPacked && '✅'}
269
269
</li>
270
270
);
271
271
```
@@ -280,7 +280,7 @@ Here it is in action:
280
280
function Item({ name, isPacked }) {
281
281
return (
282
282
<li className="item">
283
-
{name} {isPacked && '✔'}
283
+
{name} {isPacked && '✅'}
284
284
</li>
285
285
);
286
286
}
@@ -337,7 +337,7 @@ Use an `if` statement to reassign a JSX expression to `itemContent` if `isPacked
337
337
338
338
```js
339
339
if (isPacked) {
340
-
itemContent = name +"✔";
340
+
itemContent = name +"✅";
341
341
}
342
342
```
343
343
@@ -357,7 +357,7 @@ This style is the most verbose, but it's also the most flexible. Here it is in a
357
357
functionItem({ name, isPacked }) {
358
358
let itemContent = name;
359
359
if (isPacked) {
360
-
itemContent = name +"✔";
360
+
itemContent = name +"✅";
361
361
}
362
362
return (
363
363
<li className="item">
@@ -401,7 +401,7 @@ function Item({ name, isPacked }) {
401
401
if (isPacked) {
402
402
itemContent = (
403
403
<del>
404
-
{name + " ✔"}
404
+
{name + " ✅"}
405
405
</del>
406
406
);
407
407
}
@@ -464,7 +464,7 @@ Use the conditional operator (`cond ? a : b`) to render a ❌ if `isPacked` isn
464
464
functionItem({ name, isPacked }) {
465
465
return (
466
466
<li className="item">
467
-
{name} {isPacked &&'✔'}
467
+
{name} {isPacked &&'✅'}
468
468
</li>
469
469
);
470
470
}
@@ -502,7 +502,7 @@ export default function PackingList() {
Copy file name to clipboardexpand all lines: src/content/learn/you-might-not-need-an-effect.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -408,9 +408,9 @@ function Game() {
408
408
409
409
There are two problems with this code.
410
410
411
-
One problem is that it is very inefficient: the component (and its children) have to re-render between each `set` call in the chain. In the example above, in the worst case (`setCard` → render → `setGoldCardCount` → render → `setRound` → render → `setIsGameOver` → render) there are three unnecessary re-renders of the tree below.
411
+
First problem is that it is very inefficient: the component (and its children) have to re-render between each `set` call in the chain. In the example above, in the worst case (`setCard` → render → `setGoldCardCount` → render → `setRound` → render → `setIsGameOver` → render) there are three unnecessary re-renders of the tree below.
412
412
413
-
Even if it weren't slow, as your code evolves, you will run into cases where the "chain" you wrote doesn't fit the new requirements. Imagine you are adding a way to step through the history of the game moves. You'd do it by updating each state variable to a value from the past. However, setting the `card` state to a value from the past would trigger the Effect chain again and change the data you're showing. Such code is often rigid and fragile.
413
+
The second problem is that even if it weren't slow, as your code evolves, you will run into cases where the "chain" you wrote doesn't fit the new requirements. Imagine you are adding a way to step through the history of the game moves. You'd do it by updating each state variable to a value from the past. However, setting the `card` state to a value from the past would trigger the Effect chain again and change the data you're showing. Such code is often rigid and fragile.
414
414
415
415
In this case, it's better to calculate what you can during rendering, and adjust the state in the event handler:
0 commit comments