finished react tutorial
This commit is contained in:
parent
b6a60cc2a6
commit
25072921a7
11
react_test/tutorial/app/like-button.js
Normal file
11
react_test/tutorial/app/like-button.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function LikeButton() {
|
||||||
|
const [likes, setLikes] = useState(0);
|
||||||
|
function handleClick(){
|
||||||
|
setLikes(likes + 1);
|
||||||
|
}
|
||||||
|
return <button onClick={handleClick}>Likes ({likes})</button>
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
|
import LikeButton from "./like-button";
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
function Header({ title }) {
|
function Header({ title }) {
|
||||||
return <h1>{title ? title : 'Default Title'}</h1>;
|
return <h1>{title ? title : 'Default Title'}</h1>;
|
||||||
|
@ -7,11 +6,8 @@ function Header({ title }) {
|
||||||
export default function HomePage() {
|
export default function HomePage() {
|
||||||
const names = ['Ada Lovlace', 'Grace Hopper', 'Margaret Hamilton'];
|
const names = ['Ada Lovlace', 'Grace Hopper', 'Margaret Hamilton'];
|
||||||
|
|
||||||
const [likes, setLikes] = React.useState(0);
|
|
||||||
|
|
||||||
function handleClick() {
|
|
||||||
setLikes(likes + 1);
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Header title="Develop. Preview. Ship." />
|
<Header title="Develop. Preview. Ship." />
|
||||||
|
@ -20,7 +16,7 @@ export default function HomePage() {
|
||||||
<li key={name}>{name}</li>
|
<li key={name}>{name}</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<button onClick={handleClick}>Likes ({likes})</button>
|
<LikeButton />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue