[React] ๋ฆฌ์•กํŠธ์˜ Key์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž
ยท
Frontend/React
ํ•œ ํšŒ์‚ฌ์˜ ๊ธฐ์ˆ  ์ธํ„ฐ๋ทฐ์—์„œ ๋ฆฌ์•กํŠธ์˜ key์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ๋ฐ›์•˜๋‹ค. ํ•ด๋‹น ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋Œ€๋‹ต์„ ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ทธ๋™์•ˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ํ•ญ๋ชฉ์— key๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋„ˆ๋ฌด ๋‹น์—ฐ์‹œํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ์ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋ฐฐ์—ด์—์„œ ๋ฐ์ดํ„ฐ ๋ Œ๋”๋ง const people = [ { id: 0, name: 'Creola Katherine Johnson', profession: 'mathematician', }, { id: 1, name: 'Mario José Molina-Pasquel Henríquez', profession: 'chemist', }, ]; export default function List() { const listItems = people.map((person) =>..
[React] onKeyDown, onKeyUp ์ด๋ฒคํŠธ, ํ•œ๊ธ€ ์ž…๋ ฅ ์‹œ ํ•จ์ˆ˜ ๋‘ ๋ฒˆ ์‹คํ–‰๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ
ยท
Frontend/React
๐Ÿ”ฅ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์—”ํ„ฐ(Enter) ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ชฉ๋ก์— ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋˜๊ณ  input ์ฐฝ์€ ๋น„์›Œ์ง€๋„๋ก ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ์™ ์ง€ ๋ชจ๋ฅด๊ฒŒ ํ•จ์ˆ˜๊ฐ€ ๋‘ ๋ฒˆ ์‹คํ–‰๋˜๋ฉด์„œ ๊ธฐ์กด ํ…์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๊ฐ€ ๋ชฉ๋ก์— ์ถ”๊ฐ€๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹คโ—๏ธ ์›์ธ ๋ถ„์„ ๊ฒ€์ƒ‰ํ•ด๋ณธ ๊ฒฐ๊ณผ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•œ๊ธ€์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. (์˜์–ด๋กœ ์ž…๋ ฅํ•˜๋ฉด ํ‚ค ์ด๋ฒคํŠธ๊ฐ€ ์ค‘๋ณต์œผ๋กœ ๋ฐœ์ƒํ•˜์ง€ โŒ) ๊ตฌ์ฒด์ ์œผ๋กœ ์œ„ GIF๋ฅผ ๋ณด๋ฉด ํ•œ๊ธ€ ์ž…๋ ฅ ์‹œ ์ž…๋ ฅ ์ค‘์ธ ๊ธ€์ž ์•„๋ž˜ ๊ฒ€์€ ๋ฐ‘์ค„์ด ์ƒ๊ธฐ๋Š”๋ฐ ํ•ด๋‹น ๋ฐ‘์ค„์ด ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ํ‚ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋‘ ๋ฒˆ ์‹คํ–‰๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํ•œ๊ธ€์˜ ๊ฒฝ์šฐ ์ž์Œ๊ณผ ๋ชจ์Œ์˜ ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ์กฐํ•ฉ ๋ฌธ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธ€์ž๊ฐ€ ์กฐํ•ฉ ์ค‘์ธ์ง€ ์กฐํ•ฉ์ด ๋๋‚œ ์ƒํƒœ์ธ์ง€๋ฅผ ์•Œ ์ˆ˜ ์—†์–ด ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๐Ÿงฏ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ..
[React] [Immer] "An immer producer returned a new value and modified its draft. Either return a new value or modify the draft." ์—๋Ÿฌ ํ•ด๊ฒฐ
ยท
Frontend/React
๐Ÿ”ฅ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ Uncaught Error: [Immer] An immer producer returned a new value and modified its draft. Either return a new value or modify the draft. ์—๋Ÿฌ๋ฅผ ๋ฒˆ์—ญํ•˜์ž๋ฉด immer ์ œ์ž‘์ž๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์ƒํƒœ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ํ•ด์•ผ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์›์ธ์ด ๋˜๋Š” ์ฝ”๋“œ ๋ถ„์„ immer ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋ณ€์„ฑ ์ƒํƒœ์˜ ํŠธ๋ฆฌ๋ฅผ ์†์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•˜๋˜ ์ค‘ ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. updatePerson((person) => person.mentors.push({ name, title })); ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ค‘๊ด„ํ˜ธ๋ฅผ ์จ์ฃผ์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ return ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™๋Š”๋‹ค. ์ฆ‰, ์œ„์˜ ์ฝ”๋“œ๋Š” ๊ธฐ์กด..
[React/Axios] ๐Ÿคฌ Axios message: 'Network Error'(CORS ์˜ค๋ฅ˜)
ยท
Frontend/React
๐Ÿšจ ์˜ค๋ฅ˜ ๋ฐœ์ƒ AxiosError message: 'Network Error' const userInfoPatch = async (userInfo) => { try { // url: ์„œ๋ฒ„ ์ฃผ์†Œ, id: ์‚ฌ์šฉ์ž ์•„์ด๋””, userInfo: ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ์ฒด await axios.patch(`${url}/member/${id}`, userInfo).then(() => { alert('์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.'); }); } catch (error) { console.log(error); } }; ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ์—์„œ๋Š” ๋ณ„๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์—†์–ด ๋ณด์˜€๊ณ , ์„œ๋ฒ„ ์ธก์—์„œ ๋ถ„๋ช… ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ PATCH์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ GET, POST, DELETE ๋“ฑ ์—ฌ๋Ÿฌ ๋ฐฉ์‹์œผ๋กœ ์„œ๋ฒ„์— ์š”์ฒญํ• ..
[React/Axios] axios delete ์š”์ฒญ ์‹œ body์— data ๋„ฃ๋Š” ๋ฐฉ๋ฒ•
ยท
Frontend/React
๐Ÿšจ ์˜ค๋ฅ˜ ๋ฐœ์ƒ AxiosError message: 'Request failed with status code 400' axios.delete(`${url}/unlike`, { projectId: id, }) axios์˜ delete๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„์— ์š”์ฒญ์„ ํ•  ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด data๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๐Ÿ’ก ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• axios.delete๋Š” request body๋ฅผ ์ง€์›ํ•˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค. axios.delete(url[, config]) ๐Ÿ‘ config.data๋Š” request body๋ฅผ ์„ค์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. axios.delete(`${url}/unlike`, { data: { projectId: id, }, }) 2๋ฒˆ์งธ ์ธ์ž์—์„œ data๋ฅผ data: {}..
[React] React Router(๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ) ์‚ฌ์šฉํ•˜๊ธฐ
ยท
Frontend/React
SPA ๊ธฐ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•œ ์ƒํƒœ๋กœ REACT์—์„œ ํŽ˜์ด์ง€ ์ด๋™ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ React Router๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ๋ผ์šฐํŒ…์ด๋ž€? - ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•œ URL์— ๋”ฐ๋ผ ํ•ด๋‹น URL์— ๋งž๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค. - ๋ฆฌ์•กํŠธ์—๋Š” ๋ผ์šฐํŒ… ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ๊ทธ ์ค‘ ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ(React Router)๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. React Router(๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ)๋ž€ ๋ฌด์—‡์ธ๊ฐ€? - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ฃผ์†Œ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ํ™˜๊ฒฝ์—์„œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ผ์šฐํ„ฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ข…๋ฅ˜ - BrowserRouter: HTML5๋ฅผ ์ง€์›ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค. - HashRouter: ํ•ด์‹œ ์ฃผ์†Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค. React Router(๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ) ์‚ฌ์šฉํ•ด๋ณด๊ธฐ! (โ— ..
[React] SPA(Single Page Application) ์ด๋ž€?
ยท
Frontend/React
SPA๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „ SPA๋ฅผ ์™œ ์‚ฌ์šฉํ•˜๋Š”์ง€? ์ „ํ†ต์ ์ธ ์›น ์‚ฌ์ดํŠธ๋Š” ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ •์  ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋˜๊ณ  ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋ง ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒˆ๋กœ๊ณ ์นจ์ด ๋ฐœ์ƒ๋˜์–ด ์‚ฌ์šฉ์„ฑ์ด ์ข‹์ง€ ์•Š์œผ๋ฉฐ, ๋ณ€๊ฒฝ์ด ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์„ ํฌํ•จํ•˜์—ฌ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฏ€๋กœ ๊ต‰์žฅํžˆ ๋น„ํšจ์œจ์ ์ด๋‹ค. ์›น ์‚ฌ์ดํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์„œ๋ฒ„์ชฝ์—์„  ๊ณผ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ฆด ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ๊ทธ ๋งŒํผ ์†๋„์ ์ธ ์ธก๋ฉด์—์„œ ์‚ฌ์šฉ์ž๋Š” ๋ถˆํŽธํ•จ์„ ๊ทธ๋Œ€๋กœ ๋Š๋ผ๊ฒŒ ๋œ๋‹ค. ํ‹€๋ฆดํ™•๋ฅ ์ด ๋†’์€ ๊ฐœ์ธ์ ์ธ ์ง€์‹(์ˆ˜์ • ๊ฐ€๋Šฅ์„ฑ ๋†’์Œโ—) Traditional Page Lifecycle HTML → CSS → DOM → Web browser SPA Lifecycle HTML → CSS → Virtual Dom → DOM(ํ•„์š” ์š”์†Œ๋งŒ ๋ณ€๊ฒฝ) → Web browser..
[React] React.PureComponent(์„ฑ๋Šฅ ํ–ฅ์ƒ)
ยท
Frontend/React
PureComponent & memo Component์— state๋‚˜ props์— ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค๋ฉด render ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ โŒ React.PureComponent React.PureComponent๋Š” React.Component์™€ ๋น„์Šทํ•˜๋‹ค. React.Component๋Š” shouldComponentUpdate()๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์ง€๋งŒ, React.PureComponent๋Š” props์™€ state๋ฅผ ์ด์šฉํ•œ ์–•์€ ๋น„๊ต(๋ ˆํผ๋Ÿฐ์Šค๋งŒ ๋น„๊ตํ•ด์„œ ์•ˆ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ฌ๋ผ์ ธ๋„ ๋™์ผํ•œ ์˜ค๋ธŒ์ ํŠธ)๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ์ฐจ์ด์ ๋งŒ์ด ์กด์žฌํ•œ๋‹ค. ์ฐธ์กฐ๊ฐ’์ด ๋™์ผํ•˜๋ฉด ์—…๋ฐ์ดํŠธ โŒ => ShouldComponentUpdate ํ•จ์ˆ˜๋Š” false๋ฅผ ๋ฆฌํ„ด React ์ปดํฌ๋„ŒํŠธ์˜ render() ํ•จ์ˆ˜๊ฐ€ ๋™์ผํ•œ props์™€ state์— ๋Œ€ํ•˜์—ฌ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ Œ..
_์„ฑํ˜ธ_
'Frontend/React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก