๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

200316

ยท ์•ฝ 7๋ถ„

๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ๋ฆฌ์•กํŠธ

React ์ž…๋ฌธ#

Input ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ#

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” input ํƒœ๊ทธ์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

  • input์— ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋จ.

  • ์ดˆ๊ธฐํ™”๋ฅผ ํด๋ฆญํ•˜๋ฉด input ๋‚ด๋ถ€์— ๋‚ด์šฉ๋„ ์ง€์›Œ์ง€๊ณ , ์•„๋ž˜์˜ ๊ฐ’๋„ ์ง€์›Œ์ง.


InputSample.js

import React, {useState} from 'react';
function InputSample() {    const [text, setText] = useState('');
    const onChange = (e) => {        setText(e.target.value);    };
    const onReset = () => {        setText('');    };
    return (        <div>            <input onChange={onChange} value={text} />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: {text}</b>            </div>        </div>    );}
export default InputSample;

App.js

import React from 'react';import InputSample from './InputSample';
function App() {    return <InputSample />;}
export default App;

input ์˜ onChange ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉ. ์ด๋ฒคํŠธ ๊ฐ์ฒด e ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉ . e.target ์€ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ DOM์ธ input DOM์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋˜๊ณ , DOM์˜ value ๊ฐ’, e.target.value ๋ฅผ ์กฐํšŒํ•˜๋ฉด ํ˜„์žฌ input ์— ์ž…๋ ฅํ•œ ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค .


Event | PoiemaWeb


์—ฌ๋Ÿฌ ๊ฐœ์˜ input ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ#

input ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ๋‹จ์ˆœํžˆ useState ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉํ•˜๊ณ  onChange ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€ input ์— name ์„ ์„ค์ •ํ•˜๊ณ  ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ด ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ œ useState ์—์„œ๋Š” ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.


InputSample.js

import React, {useState} from 'react';
function InputSample() {    const [inputs, setInputs] = useState({        name: '',        nickname: '',    });
    const {name, nickname} = inputs; // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ†ตํ•ด ๊ฐ’ ์ถ”์ถœ
    const onChange = (e) => {        const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ        setInputs({            ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค            [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •        });    };
    const onReset = () => {        setInputs({            name: '',            nickname: '',        });    };
    return (        <div>            <input name="name" placeholder="์ด๋ฆ„" onChange={onChange} value={name} />            <input                name="nickname"                placeholder="๋‹‰๋„ค์ž„"                onChange={onChange}                value={nickname}            />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: </b>                {name} ({nickname})            </div>        </div>    );}
export default InputSample;

const onChange = (e) => {    const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ    setInputs({        ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค        [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •    });};

์ด ๋ถ€๋ถ„์„ ํ’€์–ด ์“ฐ๋ฉด

const nextInputs = {    ...inputs,};
nextInputs[name] = value;
setInputs(value);

useRef ๋กœ ํŠน์ • DOM ์„ ํƒํ•˜๊ธฐ#

JavaScript๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํŠน์ • DOM์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด getElementById , querySelector ๊ฐ™์€ DOM Selector ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ DOM์„ ์„ ํƒํ•œ๋‹ค.

๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๊ฐ€๋” DOM์„ ์ง์ ‘ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜์žˆ๋Š”๋ฐ ๊ทธ๋Ÿด ๋•Œ ref ๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ref ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” useRef ๋ผ๋Š” Hook ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ React.createRef ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” ํ˜„์žฌ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

InputSample.js ์—์„œ ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ด๋ฆ„ input ์— ํฌ์ปค์Šค๊ฐ€ ์žกํžˆ๋„๋ก ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค.

InputSample.js

import React, {useState, useRef} from 'react';
function InputSample() {    const [inputs, setInputs] = useState({        name: '',        nickname: '',    });    const nameInput = useRef();
    const {name, nickname} = inputs; // ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ†ตํ•ด ๊ฐ’ ์ถ”์ถœ
    const onChange = (e) => {        const {value, name} = e.target; // ์šฐ์„  e.target ์—์„œ name ๊ณผ value ๋ฅผ ์ถ”์ถœ        setInputs({            ...inputs, // ๊ธฐ์กด์˜ input ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋’ค            [name]: value, // name ํ‚ค๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ value ๋กœ ์„ค์ •        });    };
    const onReset = () => {        setInputs({            name: '',            nickname: '',        });        nameInput.current.focus();    };
    return (        <div>            <input                name="name"                placeholder="์ด๋ฆ„"                onChange={onChange}                value={name}                ref={nameInput}            />            <input                name="nickname"                placeholder="๋‹‰๋„ค์ž„"                onChange={onChange}                value={nickname}            />            <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>            <div>                <b>๊ฐ’: </b>                {name} ({nickname})            </div>        </div>    );}
export default InputSample;

React ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉฐ ๋Š๋‚€์ #

  • JS ๊ธฐ์ดˆ ๋ฌธ๋ฒ•๋งŒ ๋–ผ๊ณ  ๋ฐ”๋กœ React ์ž…๋ฌธ์œผ๋กœ ๋„˜์–ด์˜ค๋‹ˆ DOM, Event ๊ฐ์ฒด ๋“ฑ JS์—์„œ ๋ถ€์กฑํ–ˆ๋˜ ๊ฐœ๋…์ด ๋“ฑ์žฅํ•  ๋•Œ๋งˆ๋‹ค ๋‚ฏ์„  React ๊ตฌ์กฐ์™€ ํ•จ๊ป˜ ๋จธ๋ฆฌ ์†์„ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค .

  • ์•„์ง React์™€ JavaScript์˜ ๊ด€๊ณ„๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ฐ•์˜๋ฅผ ๋ณด๋ฉด JavaScript๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์™€ React๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ๋‹ค . React๋„ ๊ฒฐ๊ตญ JS๋‹ˆ๊นŒ JS๋กœ๋งŒ ํ•ด๋„ ๋‹ค ๋˜๋Š” ๊ฑธ๋กœ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์™œ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฑด์ง€ ์ดํ•ด๋˜์ง€ ์•Š๋Š”๋‹ค.

  • ์ข‹์€ React ๊ณต๋ถ€ ์ž๋ฃŒ๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค. ์ตœ์‹  ๊ธฐ์ˆ ์ด๋ผ ๋น ๋ฅด๊ฒŒ ๋ณ€ํ•˜๊ณ  ์žˆ์–ด์„œ ์ฑ…๋ณด๋‹ค๋Š” ์ธํ„ฐ๋„ท ์ž๋ฃŒ + ์˜จ๋ผ์ธ ๊ฐ•์˜๋กœ ๊ณต๋ถ€ํ•ด์•ผ ํ•œ๋‹ค.

    1. ๊ณต์‹ ๋ฌธ์„œ

    2. ์ƒํ™œ์ฝ”๋”ฉ - 4์‹œ๊ฐ„ 40๋ถ„ ๋ถ„๋Ÿ‰

    3. velopert ๋‹˜ ๊ฐ•์ขŒ - 2์‹œ๊ฐ„ 53๋ถ„ ๋ถ„๋Ÿ‰

    4. zeroCho ๋‹˜ ๊ฐ•์ขŒ - 12์‹œ๊ฐ„ 44๋ถ„ ๋ถ„๋Ÿ‰

    5. Udemy ๊ฐ•์ขŒ - 26์‹œ๊ฐ„ 42 ๋ถ„ ๋ถ„๋Ÿ‰


์•ž์œผ๋กœ ..#

  • zeroCho๋‹˜์˜ JavaScript ์›น๊ฒŒ์ž„ ๊ฐ•์ขŒ์™€ React ์›น๊ฒŒ์ž„ ๊ฐ•์ขŒ๊ฐ€ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด์„œ๋„์ƒ๊ฐํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ด ๋‘ ๊ฐ•์ขŒ๋ฅผ ๋“ฃ๊ณ  ๊ธฐ์ดˆ๋ฅผ ์Œ“๋Š”๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค๋””์ž์ธ ์ˆ˜์—… ๋•Œ ๋งŒ๋“  ๋ฏธ๋””์–ดํ•™๊ณผ ์ˆ˜๊ฐ•๊ณ„ํš ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์ธ React๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์ข‹์€ ์˜ˆ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.