finished react tutorial

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-19 14:16:59 +02:00
parent b6a60cc2a6
commit 25072921a7
2 changed files with 14 additions and 7 deletions

View 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>
}

View file

@ -1,5 +1,4 @@
import { useState } from 'react';
import LikeButton from "./like-button";
function Header({ title }) {
return <h1>{title ? title : 'Default Title'}</h1>;
@ -7,11 +6,8 @@ function Header({ title }) {
export default function HomePage() {
const names = ['Ada Lovlace', 'Grace Hopper', 'Margaret Hamilton'];
const [likes, setLikes] = React.useState(0);
function handleClick() {
setLikes(likes + 1);
}
return (
<div>
<Header title="Develop. Preview. Ship." />
@ -20,7 +16,7 @@ export default function HomePage() {
<li key={name}>{name}</li>
))}
</ul>
<button onClick={handleClick}>Likes ({likes})</button>
<LikeButton />
</div>
);
}