Skip to content

Commit

Permalink
Merge pull request #444 from reactjs/sync-7d50c3ff
Browse files Browse the repository at this point in the history
Sync with react.dev @ 7d50c3f
  • Loading branch information
GasimGasimzada authored Aug 28, 2024
2 parents ea0d647 + d27d6b9 commit 8c95627
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/content/blog/2023/03/16/introducing-react-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Use the conditional operator (`cond ? a : b`) to render a ❌ if `isPacked` isn
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked && 'βœ”'}
{name} {isPacked && 'βœ…'}
</li>
);
}
Expand Down Expand Up @@ -307,7 +307,7 @@ export default function PackingList() {
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked ? 'βœ”' : '❌'}
{name} {isPacked ? 'βœ…' : '❌'}
</li>
);
}
Expand Down
32 changes: 16 additions & 16 deletions src/content/learn/conditional-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ export default function PackingList() {
</Sandpack>
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}`.
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}`.
You can write this as an [`if`/`else` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) like so:
```js
if (isPacked) {
return <li className="item">{name} βœ”</li>;
return <li className="item">{name} βœ…</li>;
}
return <li className="item">{name}</li>;
```
Expand All @@ -70,7 +70,7 @@ If the `isPacked` prop is `true`, this code **returns a different JSX tree.** Wi
```js
function Item({ name, isPacked }) {
if (isPacked) {
return <li className="item">{name} βœ”</li>;
return <li className="item">{name} βœ…</li>;
}
return <li className="item">{name}</li>;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ In practice, returning `null` from a component isn't common because it might sur
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:

```js
<li className="item">{name} βœ”</li>
<li className="item">{name} βœ…</li>
```

is very similar to
Expand All @@ -172,7 +172,7 @@ Both of the conditional branches return `<li className="item">...</li>`:

```js
if (isPacked) {
return <li className="item">{name} βœ”</li>;
return <li className="item">{name} βœ…</li>;
}
return <li className="item">{name}</li>;
```
Expand All @@ -187,7 +187,7 @@ Instead of this:

```js
if (isPacked) {
return <li className="item">{name} βœ”</li>;
return <li className="item">{name} βœ…</li>;
}
return <li className="item">{name}</li>;
```
Expand All @@ -197,12 +197,12 @@ You can write this:
```js
return (
<li className="item">
{isPacked ? name + ' βœ”' : name}
{isPacked ? name + ' βœ…' : name}
</li>
);
```

You can read it as *"if `isPacked` is true, then (`?`) render `name + ' βœ”'`, otherwise (`:`) render `name`"*.
You can read it as *"if `isPacked` is true, then (`?`) render `name + ' βœ…'`, otherwise (`:`) render `name`"*.

<DeepDive>

Expand All @@ -222,7 +222,7 @@ function Item({ name, isPacked }) {
<li className="item">
{isPacked ? (
<del>
{name + ' βœ”'}
{name + ' βœ…'}
</del>
) : (
name
Expand Down Expand Up @@ -265,7 +265,7 @@ Another common shortcut you'll encounter is the [JavaScript logical AND (`&&`) o
```js
return (
<li className="item">
{name} {isPacked && 'βœ”'}
{name} {isPacked && 'βœ…'}
</li>
);
```
Expand All @@ -280,7 +280,7 @@ Here it is in action:
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked && 'βœ”'}
{name} {isPacked && 'βœ…'}
</li>
);
}
Expand Down Expand Up @@ -337,7 +337,7 @@ Use an `if` statement to reassign a JSX expression to `itemContent` if `isPacked
```js
if (isPacked) {
itemContent = name + " βœ”";
itemContent = name + " βœ…";
}
```
Expand All @@ -357,7 +357,7 @@ This style is the most verbose, but it's also the most flexible. Here it is in a
function Item({ name, isPacked }) {
let itemContent = name;
if (isPacked) {
itemContent = name + " βœ”";
itemContent = name + " βœ…";
}
return (
<li className="item">
Expand Down Expand Up @@ -401,7 +401,7 @@ function Item({ name, isPacked }) {
if (isPacked) {
itemContent = (
<del>
{name + " βœ”"}
{name + " βœ…"}
</del>
);
}
Expand Down Expand Up @@ -464,7 +464,7 @@ Use the conditional operator (`cond ? a : b`) to render a ❌ if `isPacked` isn
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked && 'βœ”'}
{name} {isPacked && 'βœ…'}
</li>
);
}
Expand Down Expand Up @@ -502,7 +502,7 @@ export default function PackingList() {
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked ? 'βœ”' : '❌'}
{name} {isPacked ? 'βœ…' : '❌'}
</li>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/content/learn/describing-the-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ In this example, the JavaScript `&&` operator is used to conditionally render a
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked && 'βœ”'}
{name} {isPacked && 'βœ…'}
</li>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/content/learn/you-might-not-need-an-effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ function Game() {
There are two problems with this code.
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.
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.
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.
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.
In this case, it's better to calculate what you can during rendering, and adjust the state in the event handler:
Expand Down
5 changes: 5 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"destination": "/learn/rendering-lists#keeping-list-items-in-order-with-key",
"permanent": false
},
{
"source": "/docs/lists-and-keys",
"destination": "/learn/rendering-lists#keeping-list-items-in-order-with-key",
"permanent": false
},
{
"source": "/link/invalid-hook-call",
"destination": "/warnings/invalid-hook-call-warning",
Expand Down

0 comments on commit 8c95627

Please sign in to comment.