๐ ๋ฒจ๋กํผํธ์ ํจ๊ปํ๋ ๋ชจ๋ ๋ฆฌ์กํธ
์๋์ ๊ธฐ๋ฅ๋ค์ ๋ชจ๋ React Hooks์ค ํ๋
on์ด๋ฒคํธ์ด๋ฆ={์คํํ๊ณ ์ถ์ํจ์}
7. useState๋ฅผ ํตํด ์ปดํฌ๋ํธ์์ ๋ฐ๋๋ ๊ฐ ๊ด๋ฆฌํ๊ธฐ
<Counter.js>
import React, { useState } from 'react';
function Counter() {
const [number, setNumber] = useState(0);
const onIncrease = () => {
setNumber(number + 1);
}
const onDecrease = () => {
setNumber(number - 1);
}
return (
<div>
<h1>{number}</h1>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
);
}
export default Counter;
(์์ธํ ๋ณด๊ธฐ) ์ด๋ ๊ฒ ํ๋ฉด, ๋ ๋๋ง๋๋ ์์ ์์ ํจ์๊ฐ ํธ์ถ๋๋ฒ๋ฆผ
onClick={onIncrease()}
(์์ธํ ๋ณด๊ธฐ) useState
const [number, setNumber] = useState(0);
์ํ์ ๊ธฐ๋ณธ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด์ ํธ์ถ
ํธ์ถํ๋ฉด ๋ฐฐ์ด์ด ๋ฐํ๋๋๋ฐ, ์ฒซ๋ฒ์งธ ์์๋ ํ์ฌ ์ํ / ๋๋ฒ์งธ ์์๋ Setter ํจ์
[ํจ์ํ ์ ๋ฐ์ดํธ]
<counter.js>
import React, { useState } from 'react';
function Counter() {
const [number, setNumber] = useState(0);
const onIncrease = () => {
setNumber(prevNumber => prevNumber + 1);
}
const onDecrease = () => {
setNumber(prevNumber => prevNumber - 1);
}
return (
<div>
<h1>{number}</h1>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
);
}
export default Counter;
onIncrease, onDecrease setNumber์์ ๊ทธ ๋ค์ ์ํ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด์ค ๊ฒ์ด ์๋๋ผ,
๊ฐ์ ์ ๋ฐ์ดํธํ๋ ํจ์๋ฅผ ๋ฃ์ด์ค
=> for ์ต์ ํ (๋์ค์ ์์๋ณด์)
8. input ์ํ ๊ด๋ฆฌํ๊ธฐ - onChange
<App.js>
import React from 'react';
import InputSample from './InputSample';
function App() {
return (
<InputSample />
);
}
export default App;
<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;
e.target : ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ DOM => input DOM
e.target.value : input ๊ฐ
(์์ธํ๋ณด๊ธฐ)
<input onChange={onChange} value={text} />
input์๋ value๋ฅผ ์ค์ ํด์ค์ผ, ์ํ๊ฐ ๋ฐ๋์์ ๋ input์ ๋ด์ฉ๋ ์ ๋ฐ์ดํธ ๋จ
9. ์ฌ๋ฌ๊ฐ์ input ์ํ ๊ด๋ฆฌํ๊ธฐ
<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;
(์์ธํ๋ณด๊ธฐ) โญโญโญ ๋ถ๋ณ์ฑ โญโญโญ
๊ฐ ์ง์ ๋ฐ๊พธ๊ธฐ XXX
inputs[name] = value;
... spread ๋ฌธ๋ฒ OOO
setInputs({
...inputs,
[name]: value
});
... spread ๋ฌธ๋ฒ : ๊ฐ์ฒด์ ๋ด์ฉ์ ๋ชจ๋ "ํผ์ณ์" ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ๋ณต์ฌ
=> ๊ธฐ์กด ๊ฐ์ฒด ์ง์ ์์ X ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ฆ
=> ๋ถ๋ณ์ฑ ์งํค๊ธฐ ์ํด
=> ๋ถ๋ณ์ฑ์ ์ง์ผ์ฃผ์ด์ผ๋ง ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์์ ์ํ๊ฐ ์ ๋ฐ์ดํธ ๋์์ ๊ฐ์งํด์ ๋ฆฌ๋ ๋๋ง ๋จ
10. useRef๋ก ํน์ DOM ์ ํํ๊ธฐ
<inputSample.js> ์ด๊ธฐํ ๋ฒํผ ํด๋ฆญํ๋ฉด input์ ํฌ์ปค์ค๊ฐ ์กํ๋๋ก
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' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ๊ณต๋ถํ๊ธฐ(4) - API ์ฐ๋ํ๊ธฐ (0) | 2021.11.05 |
---|---|
React ๊ณต๋ถํ๊ธฐ(2) - styled-components (0) | 2021.10.15 |
React ๊ณต๋ถํ๊ธฐ(1) - Hooks ์ข ๋ฅํธ (0) | 2021.10.10 |
React ๊ณต๋ถํ๊ธฐ(1) - ์ํ๊ด๋ฆฌ ไธญ ๋ฐฐ์ดํธ (0) | 2021.10.10 |
React ๊ณต๋ถํ๊ธฐ(1) - ๊ธฐ๋ณธํธ (0) | 2021.10.10 |