Say.js
const Say = () => {
const [message, setMessage] = useState("");
const onClickEnter = () => setMessage("어서와");
const onClickExit = () => setMessage("잘가");
const [color, setColor] = useState('black');
return(
<>
<button onClick={onClickEnter}>enter</button>
<button onClick={onClickExit}>exit</button>
<h1>{message}</h1>
</>
);
}
우리가 만든 컴포넌트에는 이벤트 설정 불가능함.
div, span, input, button 등에만 이벤트 설정 가능함.
<Say onClick={doSomething} />
EventPractice.js
import React, { useState } from "react";
const EventPractice = () => {
return(
<input
type='text'
name='message'
placeholder='입력'
onClick={e=>{console.log(e);}}
/>
);
}
export default EventPractice;
EventPractice.js
import React, { useState } from "react";
const EventPractice = () => {
const [message, setMessage] = useState("");
return (
<div>
<h1>이벤트 연습</h1>
<input
type='text'
name='message'
placeholder='입력'
value={message}
onChange ={ (e) => {
setMessage(e.target.value);
console.log(message);
} }
/>
</div>
);
}
export default EventPractice;
import React, { useState } from "react";
const EventPractice = () => {
const [message, setMessage] = useState("");
return (
<div>
<h1>이벤트 연습</h1>
<input
type="text"
name="message"
placeholder="입력"
value={message}
onChange={(e) => {
setMessage(e.target.value);
console.log(message);
}}
/>
<button
onClick={() => {
setMessage("");
}}
>
blank
</button>
</div>
);
const EventPractice = () => {
const [message, setMessage] = useState("");
const onChange = (e) => {
setMessage(e.target.value);
console.log(message);
};
const onClick = () => {
setMessage("");
};
return (
<div>
<h1>이벤트 연습</h1>
<input
type="text"
name="message"
placeholder="입력"
value={message}
onChange={onChange}
/>
<button onClick={onClick}>blank</button>
</div>
);
import React, { useState } from "react";
const EventPractice = () => {
const [message, setMessage] = useState("");
const [username, setUsername] = useState("");
const onChangeUsername = (e) => {
setUsername(e.target.value);
};
const onChangeMessage = (e) => {
setMessage(e.target.value);
};
const onClick = () => {
setMessage("");
setUsername("");
};
return (
<div>
<h1>이벤트 연습</h1>
<input
type="text"
name="message"
placeholder="입력"
value={message}
onChange={onChangeMessage}
/>
<input
type="text"
name="username"
placeholder="이름 입력"
value={username}
onChange={onChangeUsername}
/>
<button onClick={onClick}>blank</button>
</div>
);
import React, { useState } from "react";
const EventPractice = () => {
const [form, setForm] = useState({
username: "",
message: ""
});
const { username, message } = form;
const onChange = (e) => {
const nextForm = {
...form,
[e.target.name] : e.target.value, // 원하는 값 덮어쓰기
}
setForm(nextForm)
};
const onClick = () => {
setForm({
username: "",
message: ""
});
};
return (
<div>
<h1>이벤트 연습</h1>
<input
type="text"
name="message"
placeholder="입력"
value={message}
onChange={onChange}
/>
<input
type="text"
name="username"
placeholder="이름 입력"
value={username}
onChange={onChange}
/>
<button onClick={onClick}>blank</button>
</div>
);
{ 'e.target.name' : 'e.target.value' }