MapleStory Finger Point

๐Ÿ”ต React

[React] React Event (์ด๋ฒคํŠธ)

HYEJU01 2024. 8. 28. 20:27

1. React Event ๋ฆฌ์•กํŠธ ์ด๋ฒคํŠธ

 

๐Ÿ˜บ ๋ฆฌ์•กํŠธ์—์„œ ์ด๋ฒคํŠธ ๊ทœ์น™์„ ์•Œ์•„๋ณด์ž !

 

๐Ÿ“ ๋ฆฌ์•กํŠธ ์ด๋ฒคํŠธ ๊ทœ์น™ 

  1. ์ด๋ฒคํŠธ์˜ ์ด๋ฆ„์€ ์ „๋ถ€ ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. onkeyup -> onKeyUp
  2. ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” {ํ•จ์ˆ˜} ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค

 

 

 

๐Ÿ˜บ ์ด์ œ ๋ฆฌ์•กํŠธ ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž. ๊ธฐ๋ณธ์ ์ธ 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;