React ๊ณต๋ถ€2

์ด์ง„์šฐยท2022๋…„ 3์›” 6์ผ
0
post-custom-banner

๐Ÿ™Œ๐Ÿป ์ €๋ฒˆ์‹œ๊ฐ„์— ๊ฐ„๋‹จํ•œ ๋ฆฌ์•กํŠธ ์„ค์น˜๋ฒ•,๊ธฐ๋ณธ๊ฐœ๋…,State๊ฐœ๋…์„ ์ตํ˜€๋ณด์•˜๋Š”๋ฐ์š” ์˜ค๋Š˜์€ ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ์‚ฌ์šฉ๋ฒ•๊ณผ state๋ณ€๊ฒฝ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~~

<div>
	<h3> ์ถ”์ฒœ์ˆ˜ <span onClick={}>๐Ÿ‘๐Ÿป</span></h3>
</div>

์ €๋ฒˆ๊ธ€์—๋„ ๋งํ–ˆ๋“ฏ์ด jsx๋ฌธ๋ฒ•์„ ์“ฐ๋ ค๋ฉด
๊ธฐ์กด --> onClick={}

let [good,goodfx] = useState(0)
<div>
	<h3> ์ถ”์ฒœ์ˆ˜ <span onClick={ ()=>{goodfx(good+1)} }>๐Ÿ‘๐Ÿป</span></h3>
</div>

๊ทธ๋Ÿฐ๋ฐ!! ์—ฌ๊ธฐ์„œ ์ฒ˜์Œ์œผ๋กœ state๋ฅผ ๋‹ด์€ ๋ณ€์ˆ˜์— ๋‘๋ฒˆ์จฐ state๋ณ€๊ฒฝ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋Š”๋ฐ์š” ์ด๋ ‡๊ฒŒ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ฒ˜๋Ÿผ ()=>{ ๋ณ€๊ฒฝํ•จ์ˆ˜() } ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ์—ฐ์‚ฐ์ด๋‚˜ ์ž‘์—…์€ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

State๋ณ€๊ฒฝํ•จ์ˆ˜๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์‚ฌ์šฉํ•ด ๋ณด์ž

์œ„์˜ ์„ค๋ช…์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด์žˆ๋‹ค.

let [store,storefx] = useState['nike','adidas','vans']

function changeStore() {
	let stores = store
    // ํ‹€๋ฆฐ๋ณต์‚ฌ
}

<h3>{store[0]}<button <onClick={ changeStore }>๋งค์žฅ๋ณ€๊ฒฝ</button></h3>

์ด๋Ÿฐ์‹์œผ๋กœ onClick์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ์™ธ๋ถ€์—์„œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š”๋ฐ ์ฃผ์˜ํ• ์ ์€ ํ•จ์ˆ˜๋ช… ๋’ค์— ()์†Œ๊ด„ํ˜ธ๋ฅผ ๋ถ™ํžˆ์ง€ ์•Š๋Š”๋‹ค.
๊ด„ํ˜ธ๋ฅผ ๋ถ™ํžˆ๋ฉด ๋ˆ„๋ฅด๊ธฐ์ „์— ๊ทธ๋ƒฅ ์‹คํ–‰์ด ๋˜์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— onClick์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ ํ•จ์ˆ˜ํ˜ธ์ถœ์„ ํ•˜๊ธฐ์œ„ํ•ด ๊ด„ํ˜ธ๋ฅผ ๋นผ๋Š”๊ฒƒ์ด๋‹ค

๋˜ํ•œ ์œ„์— state์—์„œ store์•ˆ์˜ ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•ด์˜ฌ๋•Œ ๊ทธ๋ƒฅ ์ €๋ ‡๊ฒŒ store๋กœ ๋‹ด๊ฒŒ๋˜๋ฉด ๊ฐ’๋ณต์‚ฌ๊ฐ€ ์•„๋‹Œ ๊ฐ’๊ณต์œ ๊ฐ€๋˜๊ณ  ์›๋ณธ state๋Š” ๋ณ€๊ฒฝํ•˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— deepcopy๋ฅผ ์‚ฌ์šฉํ•ด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค
(๊ฐ์ฒดobject๋Š” {...store} ์ด๋ ‡๊ฒŒ ๋ณต์‚ฌ๋ฅผ ํ•ด์ค€๋‹ค.)

let [store,storefx] = useState['nike','adidas','vans']

function changeStore() {
	let stores = [...store];
    stores[0] = 'Jordan';
    change(stores)
}

<h3>{store[0]}<button onClick={ changeStore }>๋งค์žฅ๋ณ€๊ฒฝ</button>
profile
์ดˆ๋ณด๊ฐœ๋ฐœ์ž์˜ ๊ฐœ๋ฐœ์ผ๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€