header-img
Info :
728x90

์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ ํผ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์„œ ์–ด๋–ค ๊ณณ์— send ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌผ๋ก  useref ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์—ฌ ๋ณด๋‚ด์–ด๋„ ๋œ๋‹ค.

 

๋งŒ์•ฝ ์–ด๋–ค event ๊ฐ€ ์ผ์–ด๋‚  ๋•Œ๋งˆ๋‹ค usestate ์ด์ „ ๊ฐ’์— ์ถ”๊ฐ€ํ•˜์—ฌ ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚˜๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด...

 

setUser((prev)=>([...prev, ...add1]))
setUser((prev)=>([...prev, ...add2]))

 

์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

CASE 1) ํŠน์ • ๊ฐ’ ๋ˆ„์ ํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ธฐ

const [number, setNumber] = useState(0)
function onClick() {
      setNumber(prev => prev + 1) // ์ƒํƒœ๊ฐ’ ๋ณ€๊ฒฝ ํ•จ์ˆ˜์— ํ•จ์ˆ˜ ์ „๋‹ฌ
    }

 

CASE 2) ์ž…๋ ฅ ํผ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ˆ„์  ์‹œํ‚ค๊ธฐ

const [formData, setFormData] = useState({});
function onClick() {
      setFormData((prev) => ({
      ...prev,
      [name]: value
    }));
    }
728x90
๋”๋ณด๊ธฐ
FRONTEND/React