1. React Event ๋ฆฌ์กํธ ์ด๋ฒคํธ
๐บ ๋ฆฌ์กํธ์์ ์ด๋ฒคํธ ๊ท์น์ ์์๋ณด์ !
๐ ๋ฆฌ์กํธ ์ด๋ฒคํธ ๊ท์น
- ์ด๋ฒคํธ์ ์ด๋ฆ์ ์ ๋ถ ์นด๋ฉ ํ๊ธฐ๋ฒ์ผ๋ก ํํ๋ฉ๋๋ค. onkeyup -> onKeyUp
- ์ด๋ฒคํธ๋ฅผ ์ ๋ฌํ ๋๋ {ํจ์} ํํ๋ก ์ฌ์ฉํฉ๋๋ค
๐บ ์ด์ ๋ฆฌ์กํธ ์ด๋ฒคํธ๋ฅผ ์ฌ์ฉํด๋ณด์. ๊ธฐ๋ณธ์ ์ธ Input ๊ฐ๋ถํฐ ํธ๋ค๋ง ํด๋ณด์ !
๐ Input ๊ฐ ํธ๋ค๋ง
const EventComponent = () => {
const [name, setName] = useState('');
let handleName = (e) => {//2.event๊ฐ์ฒด ํ์ฉ
setName(e.target.value);//3.state๋ณ๊ฒฝ (input์ value๋ ๋ณ๊ฒฝ)
}
return (
<div>
<h3>๋ฆฌ์กํธ ์ด๋ฒคํธ ํธ๋ค๋ง</h3>
<input type="text" name="name" onChange={handleName} value={name}/><br/> {/*1. ์ด๋ฒคํธ์ฐ๊ฒฐ*/}
<h3>์ฒด์ธ์ง๋ ๊ฒฐ๊ณผ: {name}</h3>
</div>
)
}
๐ useState ๋ฅผ ํ๋๋ก ๊ด๋ฆฌํ๋ค (๊ฐ์ฒด๋ก ์ฌ์ฉ)
const EventComponent2 = () => {
const [form, setForm] = useState({name: '', topic: ''});//๊ฐ์ฒด state
let handleChange = (e) => {
{...data, ํค : ๊ฐ}
const copy ={...form, [e.target.name]: e.target.value }//spread๋ฌธ๋ฒ์ผ๋ก ๋ณต์ฌ ํ ํค ๊ฐ ์์
setForm(copy);//state๋ณ๊ฒฝ
}
let handleClick = () => {
alert(`๊ฒฐ๊ณผ: ${form.name} ๋์ ํ ์ผ: ${form.topic}`)
setForm({name: '', topic: ''})//state๋ณ๊ฒฝ
}
return (
<div>
<h3>๋ฆฌ์กํธ ์ด๋ฒคํธ ํธ๋ค๋ง(๊ฐ์ฒด๋ก ํธ๋ค๋ง)</h3>
<input type="text" name="name" onChange={handleChange} value={form.name}/><br/>
<h3>์ฒด์ธ์ง๋ ๊ฒฐ๊ณผ: </h3>
<input type="text" name="topic" onChange={handleChange} value={form.topic} /><br/>
<h3>์ฒด์ธ์ง๋ ๊ฒฐ๊ณผ: </h3>
<button type="button" onClick={handleClick}>ํด๋ฆญ๋ฏธ</button>
</div>
)
}
๐ ๋ฆฌ์กํธ ์ด๋ฒคํธ ์ค์ต
[ App.js ]
import { Fragment } from "react";
import EventComponent from "./component/EventComponent";
import EventComponent2 from "./component/EventComponent2";
import EventComponentQ from "./component/EventComponentQ";
function App() {
return (
<Fragment>
<EventComponent />
<EventComponent2 />
<EventComponentQ/>
</Fragment>
)
}
export default App;
[ EventComponent.js ]
import { useState } from "react";
function EventComponent(){
//let a = document.querySelector("input[name=name]");
//console.log(a.value);
// ๋ฆฌ์กํธ๊ฐ ์ ํธํ๋ ๋ฐฉ์์ ์๋๋ค
//1 input ์ด ๊ด๋ฆฌํ ๊ฐ์ state ๋ก ์ ์ธํ๋ค.
let [name, setName] = useState('');
let [topic, setTopic] = useState('');
let [option, setOption] = useState('');
//2 ์ธํํ๊ทธ value ์ state ์ฐ๊ฒฐ
//3 ์ด๋ฒคํธ ๋ฅผ ํตํด์ inpur ๊ฐ ์ ์ด
let handleName = (e) => {
console.log("์ฒด์ธ์ง"); // state ๋ ์ฌ์ ํ ๊ณต๋ฐฑ์
console.log(e); // ์ด๋ฒคํธ์ ๊ด๋ จ๋
console.log(e.target.value);
setName(e.target.value);
}
let handleTopic = (e) => {
setTopic(e.target.value);
}
let hadleClick = () => {
alert(`${name} ๋์ ์
๋ ฅ๊ฐ์ ${topic}`);
setName('');
setTopic('');
}
let handleKeyUp = (e) => {
console.log('ํค๋ณด๋ ๋์');
if (e.key === 'Enter'){
hadleClick();
}
}
let handleSelect = (e) => {
console.log(e);
console.log('select ๋์');
setOption(e.target.value);
}
return (
<div>
hello world
<hr />
<input type="text" name="name" value={name} onChange={handleName}/>
๊ฒฐ๊ณผ state ๊ฐ : {name}
<hr />
<input type="text" name="topic" value={topic} onChange={handleTopic} onKeyUp={handleKeyUp} />
๊ฒฐ๊ณผ state ๊ฐ : {topic}
<hr />
<button type="button" onClick={hadleClick}> ํด๋ฆญ ์ด๋ฒคํธ </button>
<hr/>
<h3>์
๋ ํธ ํ๊ทธ ํธ๋ค๋ง(์ค์ต)</h3>
<pre>์
๋ ํธ ํ๊ทธ๊ฐ ์ฒด์ธ์ง ๋ ๋ ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ์๋์ ์ถ๋ ฅ</pre>
<select onChange={handleSelect} value={option}>
<option>ํผ์</option>
<option>ํ๋ฒ๊ฑฐ</option>
<option>์นํจ</option>
</select>
<h3> ์ ํํ ๊ฒฐ๊ณผ : {option} </h3>
<p></p>
</div>
)
}
export default EventComponent;
let handleKeyUp = (e) => {
console.log('ํค๋ณด๋ ๋์');
if (e.key === 'Enter'){
hadleClick();
}
}
- ์ํฐ๊ฐ ๋๋ฆฌ๋ฉด handleClick() ์คํ์ํจ๋ค๋ ๋ป onKeyUp ์ ์ฌ์ฉํ๋ค.
let handleSelect = (e) => {
console.log(e);
console.log('select ๋์');
setOption(e.target.value);
}
- ์ ํ๋ e.target ์ value ๊ฐ์ option ์ ์ ์ฅ์ํจ๋ค !
[ EventComponent2.js ]
import { useState } from "react";
function EventComponent2(){
let [form, setForm] = useState({name : '', topic : ''});
let handleForm = (e) => {
console.log(e.target.name);
console.log(e.target.value);
let copy = {...form ,[e.target.name] : e.target.value} // ํ์ฌ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ ๋ณต์ฌํ๋ค.
setForm(copy);
console.log(copy);
}
let handleClick = () => {
alert(`${form.name} ๋์ ํ ์ผ ${form.topic}`);
setForm({name:'', topic:''});
}
return (
<div>
<h3> ์ธํํ๊ทธ ๊ฐ์ฒด๋ก ํธ๋ค๋งํ๊ธฐ </h3>
<hr />
<input type="text" name="name" value={form.name} onChange={handleForm}/>
๊ฒฐ๊ณผ state ๊ฐ : {form.name}
<hr />
<input type="text" name="topic" value={form.topic} onChange={handleForm} />
๊ฒฐ๊ณผ state ๊ฐ : {form.topic}
<br />
<button onClick={handleClick}> gg </button>
<hr />
</div>
)
}
export default EventComponent2;
let [form, setForm] = useState({name : '', topic : ''});
- { name: '', topic: '' }๋ผ๋ ๊ฐ์ฒด๋ก ์ด๊ธฐํ
let copy = {...form ,[e.target.name] : e.target.value} // ํ์ฌ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ ๋ณต์ฌํ๋ค.
- form ๊ฐ์ฒด์ ํ์ฌ ์ํ๋ฅผ ๊ทธ๋๋ก ์๋ก์ด ๊ฐ์ฒด copy๋ก ๋ณต์ฌ
- , form ๊ฐ์ฒด์ ํน์ ํ๋๋ฅผ ์ ๋ฐ์ดํธ
[ EventComponentQ.js ]
import { useState } from "react";
function EventComponentQ(){
let [data, setData] = useState('');
let [result, setResult] = useState('');
let handleForm = (e) => {
setData(e.target.value);
}
let handleClick = () => {
setData('');
setResult(data);
}
return (
<div>
<h1> ์ธํ ๋ฐ์ดํฐ ํธ๋ค๋ง </h1>
ํด๋ฆญ์ ๋ฐ์ดํฐ๋ ๊ณต๋ฐฑ์ผ๋ก
๊ฒฐ๊ณผ๋ ์ธํ์ด ์
๋ ฅํ ๊ฐ
*state ๋๊ฐ ํ์ํ ๋ฏ ๊ฐ์ฒด๋ก
<br/>
<input type="text" value={data} onChange={handleForm}/>
<input type="button" name="result" value="์ถ๊ฐํ๊ธฐ" onClick={handleClick}/>
<br/>
<h1> ๊ฒฐ๊ณผ </h1>
<div> {result} ์
๋๋ค. </div>
</div>
)
}
export default EventComponentQ;
import { useState } from "react";
function EventComponentQ(){
let [data, setData] = useState({data:'', result:''});
let handleChange= (e) => {
setData({data: e.target.value ,result: data.result});
}
let handleClick = (e) => {
setData({data:'',result : data.data});
}
return (
<div>
<h1> ์ธํ ๋ฐ์ดํฐ ํธ๋ค๋ง </h1>
ํด๋ฆญ์ ๋ฐ์ดํฐ๋ ๊ณต๋ฐฑ์ผ๋ก
๊ฒฐ๊ณผ๋ ์ธํ์ด ์
๋ ฅํ ๊ฐ
*state ๋๊ฐ ํ์ํ ๋ฏ ๊ฐ์ฒด๋ก
<br/>
<input type="text" value={data.data} onChange={handleChange}/>
<input type="button" name="result" value="์ถ๊ฐํ๊ธฐ" onClick={handleClick}/>
<br/>
<h1> ๊ฒฐ๊ณผ </h1>
<div> {data.result} ์
๋๋ค. </div>
</div>
)
}
export default EventComponentQ;