<form>
<input type="text" placeholder="color title..." required />
<input type="color" required />
<button>ADD</button>
</form>
export default function Button() {
return (
<button>
I don't do anything
</button>
);
}
handleClick inside your Button component.alert to show the message).onClick={handleClick} to the <button> JSX.export default function Button() {
function handleClick() {
alert('You clicked me!');
}
return (
<button onClick={handleClick}>
Click me
</button>
);
}
handle, followed by the name of the event.handle followed by the event name<button onClick={function handleClick() {
alert('You clicked me!');
}}
<button onClick={() => {
alert('You clicked me!');
}}/>
function AlertButton({ message, children }) {
return (
<button onClick={() => alert(message)}>
{children}
</button>
);
}
export default function Toolbar() {
return (
<div>
<AlertButton message="Playing!">Play Movie</AlertButton>
<AlertButton message="Uploading!">Upload Image</AlertButton>
</div>
);
}

태그 사이의 내용이 children prop에 들어간 것을 확인 할 수 있다.
function Button({ onClick, children }) {
return (
<button onClick={onClick}>
{children}
</button>
);
}
function PlayButton({ movieName }) {
function handlePlayClick() {
alert(`Playing ${movieName}!`);
}
return (
<Button onClick={handlePlayClick}>
Play "{movieName}“
</Button>
);
}
function UploadButton() {
return (
<Button onClick={() => alert('Uploading!')}>
Upload Image
</Button>
);
}
export default function Toolbar() {
return (
<div>
<PlayButton movieName="Kiki's Delivery Service" />
<UploadButton />
</div>
);
}
PlayButton and an UploadButton:PlayButton passes handlePlayClick as the onClick prop to the Button inside.UploadButton passes () => alert('Uploading!') as the onClick prop to the Button inside<button>’s onClick will run first, followed by the parent <div>’s onClickexport default function Toolbar() {
return (
<div className="Toolbar" onClick={() => {
alert('You clicked on the toolbar!');
}}>
<button onClick={() => alert('Playing!')}>Play Movie</button>
<button onClick={() => alert('Uploading!')}>Upload Image</button>
</div>
);
}
playing 출력 후 you clicked on the toolbar!가 출력된다
function Button({ onClick, children }) {
return (
<button onClick={e => {
e.stopPropagation();
onClick();
}}>
{children}
</button>
);
}
export default function Toolbar() {
return (
<div className="Toolbar" onClick={() => {
alert('You clicked on the toolbar!');
}}>
<Button onClick={() => alert('Playing!')}>
Play Movie
</Button>
<Button onClick={() => alert('Uploading!')}>
Upload Image
</Button>
</div>
);
}
e.StopPropagation() will stop the event propagatedonClick handler defined in Button does the following:e.stopPropagation(), preventing the event from bubbling further.<div>’s onClick handler does not run.playing만 출력된다.
import { sculptureList } from './data.js';
export default function Gallery() {
let index = 0;
function handleClick() {
index = index + 1;
}
let sculpture = sculptureList[index];
return (
<>
<button onClick={handleClick}>Next</button>
<h2>
<i>{sculpture.name} </i> by {sculpture.artist}
</h2>
<h3>
({index + 1} of {sculptureList.length})
</h3>
<img src={sculpture.url} alt={sculpture.alt} />
<p>
{sculpture.description}
</p>
</>
);
}
import { useState } from 'react';
import { sculptureList } from './data.js’;
export default function Gallery() {
const [index, setIndex] = useState(0);
function handleClick() {
setIndex(index + 1);
}
let sculpture = sculptureList[index];
return (
<>
<button onClick={handleClick}>Next</button>
<h2>
<i>{sculpture.name} </i> by {sculpture.artist}
</h2>
<h3>
({index + 1} of {sculptureList.length})
</h3>
<img src={sculpture.url} alt={sculpture.alt} />
<p>
{sculpture.description}
</p>
</>
);
}
const [index, setIndex] = useState(0);useState returns an array of two elements[ ] is array destructing (give values from an array)setIndex(newValue) : setIndex is responsible for updating and newValue will be the new indeximport { useState } from 'react';
import { sculptureList } from './data.js’;
export default function Gallery() {
const [index, setIndex] = useState(0);
const [showMore, setShowMore] = useState(false);
function handleClick() {
setIndex(index + 1);
}
function handleMoreClick() {
setShowMore(!showMore);
}
let sculpture = sculptureList[index];
return (
<>
<button onClick={handleClick}>Next</button>
<h2>
<i>{sculpture.name} </i> by {sculpture.artist}
</h2>
<h3>
({index + 1} of {sculptureList.length})
</h3>
<button onClick={handleMoreClick}>
{showMore ? 'Hide' : 'Show'} details
</button>
{showMore && <p>{sculpture.description}</p>}
<img
src={sculpture.url}
alt={sculpture.alt}
/>
</>
);
}