Skip to content

Commit c4d2a6d

Browse files
committed
[Beta] Reorder example
1 parent d542994 commit c4d2a6d

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

beta/src/content/learn/index.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ If you render the same component multiple times, each will get its own state. Tr
344344
```js
345345
import { useState } from 'react';
346346

347+
export default function MyApp() {
348+
return (
349+
<div>
350+
<h1>Counters that update separately</h1>
351+
<MyButton />
352+
<MyButton />
353+
</div>
354+
);
355+
}
356+
347357
function MyButton() {
348358
const [count, setCount] = useState(0);
349359

@@ -357,16 +367,6 @@ function MyButton() {
357367
</button>
358368
);
359369
}
360-
361-
export default function MyApp() {
362-
return (
363-
<div>
364-
<h1>Counters that update separately</h1>
365-
<MyButton />
366-
<MyButton />
367-
</div>
368-
);
369-
}
370370
```
371371
372372
```css
@@ -432,11 +432,7 @@ Now when you click either button, the `count` in `MyApp` will change, which will
432432
433433
First, *move the state up* from `MyButton` into `MyApp`:
434434
435-
```js {2,6-10}
436-
function MyButton() {
437-
// ... we're moving code from here ...
438-
}
439-
435+
```js {2-6,18}
440436
export default function MyApp() {
441437
const [count, setCount] = useState(0);
442438

@@ -452,6 +448,11 @@ export default function MyApp() {
452448
</div>
453449
);
454450
}
451+
452+
function MyButton() {
453+
// ... we're moving code from here ...
454+
}
455+
455456
```
456457
457458
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
497498
```js
498499
import { useState } from 'react';
499500

500-
function MyButton({ count, onClick }) {
501-
return (
502-
<button onClick={onClick}>
503-
Clicked {count} times
504-
</button>
505-
);
506-
}
507-
508501
export default function MyApp() {
509502
const [count, setCount] = useState(0);
510503

@@ -520,6 +513,14 @@ export default function MyApp() {
520513
</div>
521514
);
522515
}
516+
517+
function MyButton({ count, onClick }) {
518+
return (
519+
<button onClick={onClick}>
520+
Clicked {count} times
521+
</button>
522+
);
523+
}
523524
```
524525
525526
```css

0 commit comments

Comments
 (0)