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: beta/src/content/learn/index.md
+24-23Lines changed: 24 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -344,6 +344,16 @@ If you render the same component multiple times, each will get its own state. Tr
344
344
```js
345
345
import { useState } from'react';
346
346
347
+
exportdefaultfunctionMyApp() {
348
+
return (
349
+
<div>
350
+
<h1>Counters that update separately</h1>
351
+
<MyButton />
352
+
<MyButton />
353
+
</div>
354
+
);
355
+
}
356
+
347
357
functionMyButton() {
348
358
const [count, setCount] =useState(0);
349
359
@@ -357,16 +367,6 @@ function MyButton() {
357
367
</button>
358
368
);
359
369
}
360
-
361
-
exportdefaultfunctionMyApp() {
362
-
return (
363
-
<div>
364
-
<h1>Counters that update separately</h1>
365
-
<MyButton />
366
-
<MyButton />
367
-
</div>
368
-
);
369
-
}
370
370
```
371
371
372
372
```css
@@ -432,11 +432,7 @@ Now when you click either button, the `count` in `MyApp` will change, which will
432
432
433
433
First, *move the state up* from `MyButton` into `MyApp`:
434
434
435
-
```js {2,6-10}
436
-
functionMyButton() {
437
-
// ... we're moving code from here ...
438
-
}
439
-
435
+
```js {2-6,18}
440
436
exportdefaultfunctionMyApp() {
441
437
const [count, setCount] =useState(0);
442
438
@@ -452,6 +448,11 @@ export default function MyApp() {
452
448
</div>
453
449
);
454
450
}
451
+
452
+
functionMyButton() {
453
+
// ... we're moving code from here ...
454
+
}
455
+
455
456
```
456
457
457
458
Then, *pass the state down* from `MyApp` to each `MyButton`, together with the shared click handler. You can pass information to `MyButton` using the JSX curly braces, just like you previously did with built-in tags like `<img>`:
@@ -497,14 +498,6 @@ This is called "lifting state up". By moving state up, we've shared it between c
497
498
```js
498
499
import { useState } from'react';
499
500
500
-
functionMyButton({ count, onClick }) {
501
-
return (
502
-
<button onClick={onClick}>
503
-
Clicked {count} times
504
-
</button>
505
-
);
506
-
}
507
-
508
501
exportdefaultfunctionMyApp() {
509
502
const [count, setCount] =useState(0);
510
503
@@ -520,6 +513,14 @@ export default function MyApp() {
0 commit comments