[ToyProject-Todomate] ํˆฌ๋‘๋ฉ”์ดํŠธ ํด๋ก  ํ”„๋กœ์ ํŠธ 27

2022. 1. 15. 20:38ใ†WEB Dev/ToyProject

728x90


๐Ÿ”ท CloneTodo โ˜‘ - Todomate Clone Project  |  Team CloneMate

 

CloneTodo : ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ๋ž‘ํ•˜๋Š” ํˆฌ๋‘๋ฉ”์ดํŠธ๋ฅผ ํด๋ก ํ•˜์—ฌ ์›น ์„œ๋น„์Šค๋ฅผ ๋ฐฐํฌํ•ด๋ณด๋Š” ํ”„๋กœ์ ํŠธ

 


 

 

์–ด์ œ ๋ชฉํ‘œ ์ถœ๋ ฅ ํ™”๋ฉด์€ ์ •๋ฆฌ๊ฐ€ ๋˜์—ˆ๊ณ  ์ด์ œ ์ž‘์„ฑ๋œ ๋ชฉํ‘œ๋ฅผ ํด๋ฆญํ•˜๋ฉด Input์— ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ณ  ๊ทธ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋ฉด ํ•ด๋‹น ๋ชฉํ‘œ๊ฐ€ ์ˆ˜์ •๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

์–ด๋–ป๊ฒŒ ํ•˜๋ฉด Input ํƒœ๊ทธ์— ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๋ณด๋‹ˆ ์ด์ „์— PHP๋กœ ํšŒ์›๊ฐ€์ž…์„ ๋งŒ๋“ค์—ˆ๋˜ ํ™”๋ฉด์ด ๋– ์˜ฌ๋ž๋‹ค.

ํšŒ์›๊ฐ€์ž… ํ›„ ํšŒ์›์ •๋ณด์ˆ˜์ •์— ๋“ค์–ด๊ฐ€๋ฉด ํšŒ์› ์•„์ด๋””๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ •๋ณด๋ฅผ ์ฐพ์•„ value์— ๋ฟŒ๋ ค์คฌ์—ˆ๋‹ค.

 

์ด๊ฒƒ๋„ ๊ทธ๊ฒƒ๊ณผ ๊ฐ™์ง€ ์•Š์„๊นŒ..

 

 

Input ํƒœ๊ทธ์— title ๊ฐ’ ๋ฟŒ๋ฆฌ๊ธฐ

 

 

์šฐ์„  ํด๋ฆญํ•˜๋ฉด editId๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” Input์— ๋ฟŒ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณด์ž.

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— editToGoalForm ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” state๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ App.js ์— ์žˆ์œผ๋ฉด ๋˜๊ฒ ๋‹ค.

 

 

//๋ชฉํ‘œ GoalForm์œผ๋กœ ๋ณด๋‚ด๊ธฐ ํ•จ์ˆ˜
function editToGoalForm(id){
	const thisItems = goal.items; // goal State ์›๋ณธ ์นดํ”ผ

}

 

 

์ธ์ž๋กœ ๋“ค์–ด์˜จ id๋ฅผ ์ด์šฉํ•ด ํ•ด๋‹น state๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

ํ˜„์žฌ state์˜ ์›๋ณธ์„ ์นดํ”ผํ•˜๊ธฐ ์œ„ํ•ด thisItems ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด์ค€๋‹ค. ๋Š” ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ id๊ฐ’์ด ์ฃผ์–ด์ง„ ์ธ์ž์™€ ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋Š”์ง€ ์ฐพ์•„๋ณด์ž.

 

thisItems๋ฅผ ์ฝ˜์†”๋กœ ์ฐ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

 

๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๋ฐ˜๋ณต๋ฌธ์„ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ์กฐ๊ฑด์„ ์“ธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— deleteGoal ์ฒ˜๋Ÿผ filter ํ•จ์ˆ˜๋ฅผ ์จ๋ด์•ผ๊ฒ ๋‹ค.

 

 

 

   //๋ชฉํ‘œ GoalForm์œผ๋กœ ๋ณด๋‚ด๊ธฐ ํ•จ์ˆ˜
   function editToGoalForm(id){
    const thisItems = goal.items; // goal State ์›๋ณธ ์นดํ”ผ
    const editItems = thisItems.filter(e => e.id === id)
    console.log(editItems)
  }

 

 

์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ฐ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

 

 

 

๊ทธ๋Ÿผ ์ด์ œ ์ด ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  editItems๋ผ๋Š” ๋ฐฐ์—ด์„ Input์— ์ „๋‹ฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์•„์ง๋„ ์ „๋‹ฌ์„ ๋ชปํ–ˆ๋‹ค.

 

๋งŒ๋“  editItems๋ฅผ GoalForm์— ์ „๋‹ฌํ•ด์ฃผ๊ณ  ์‹ถ์€๋ฐ...

Goals์—๋‹ค ๋˜ ์ „๋‹ฌํ•˜๊ณ  GoalForm์œผ๋กœ ๋˜ ๊ฐ€์•ผํ• ๊นŒ?

๊ทธ๋ƒฅ App.js์— ์žˆ๋Š” Goals ์ปดํฌ๋„ŒํŠธ ์œ„์— ๋„ฃ์–ด๋„ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ.. ํ•จ์ˆ˜๋ฅผ ๋ณด๋‚ด๊ธฐ๋„ ๋” ํŽธํ•˜๊ณ .

 

<Route exact path="/goals">
        <BasicNavBar/>
          <GoalForm addGoal={addGoal} editTmpGoal={editTmpGoal}  />
          <Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal}
          editToGoalForm={editToGoalForm}  />
        </Route>

 

Goals ์ปดํฌ๋„ŒํŠธ์—์„œ GoalsForm ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋นผ๊ณ  App.js ์— ๋ผ์šฐํ„ฐ ๋‚ด์˜ ๋ฐฐ์น˜๋ฅผ ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ์—ˆ๋‹ค.

 

 

 

์‚ฌ์‹ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋งŒ๋“ค๋ผ๊ณ  ํ•˜๋ฉด ๊ธˆ๋ฐฉ ๋งŒ๋“ค๊ฒ ๋Š”๋ฐ ๋ฆฌ์•กํŠธ๋กœ ํ•˜๋‹ˆ๊นŒ ์ปดํฌ๋„ŒํŠธ์˜ ์ˆœ์„œ๋„ ๋งž์ถฐ์•ผํ•˜๊ณ , props๋„ ๋‚ด๋ ค์•ผ ํ•˜๊ณ  ... ๊ณ ๋ฏผ์ด ์ด๋งŒ์ €๋งŒ์ด ์•„๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ์—๋Š” ์™œ ๋ฆฌ์•กํŠธ๋ฅผ ์“ฐ๊ฒŒ ๋˜๋Š”์ง€ ์•Œ๊ฒŒ ๋œ๋‹ค๋Š”๋ฐ..

 

์ง€๊ธˆ ํ•ด์•ผํ•  ์ผ์€ ๋ชฉํ‘œ ํด๋ฆญํ•˜๋ฉด! textfield์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

๋‹น์—ฐํžˆ DOM์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—  textfield๋ฅผ ์ฟผ๋ฆฌ์…€๋ ‰ํ„ฐ๊ฐ™์€๊ฑธ๋กœ ์„ ํƒํ•ด์•ผํ•˜๋‚˜? ํ–ˆ๋”๋‹ˆ

๋˜ ํ›…์ด ์žˆ๋‹ค.

 

ํ›…..

 

๊ทธ๋ฆฌ๊ณ  ์ˆ˜์ • ์ƒํƒœ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ, textfield์— value๊ฐ€ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ

๊ทธ๊ฒƒ๋ณด๋‹ค๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ ์ด๊ณ , ์ €๋ฒˆ์— ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ readOnly ๊ฐ€ false์ผ ๋•Œ, ์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ธฐ์กด GoalForm ์ปดํฌ๋„ŒํŠธ ์œ„์— ๋ฎ์ด๋Š”๊ฒŒ ๋‚ซ์ง€ ์•Š์„๊นŒ?

 

 

์šฐ์„  useRef ๋จผ์ € ์จ๋ด์•ผ๊ฒ ๋‹ค.

 

 

useRef ์‚ฌ์šฉํ•˜๊ธฐ

 

 

useRef ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

 

 

 

const goalField = useRef;

 

 

 

๊ทธ๋ฆฌ๊ณ  ๋ณ€๊ฒฝ์„ ์œ„ํ•˜๋Š” ํƒœ๊ทธ์— ์†์„ฑ์œผ๋กœ ref ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.


๐Ÿ”ท CloneTodo โ˜‘ - Todomate Clone Project  |  Team CloneMate

 

CloneTodo : ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ๋ž‘ํ•˜๋Š” ํˆฌ๋‘๋ฉ”์ดํŠธ๋ฅผ ํด๋ก ํ•˜์—ฌ ์›น ์„œ๋น„์Šค๋ฅผ ๋ฐฐํฌํ•ด๋ณด๋Š” ํ”„๋กœ์ ํŠธ

 


 

 

์–ด์ œ ๋ชฉํ‘œ ์ถœ๋ ฅ ํ™”๋ฉด์€ ์ •๋ฆฌ๊ฐ€ ๋˜์—ˆ๊ณ  ์ด์ œ ์ž‘์„ฑ๋œ ๋ชฉํ‘œ๋ฅผ ํด๋ฆญํ•˜๋ฉด Input์— ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ณ  ๊ทธ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋ฉด ํ•ด๋‹น ๋ชฉํ‘œ๊ฐ€ ์ˆ˜์ •๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

์–ด๋–ป๊ฒŒ ํ•˜๋ฉด Input ํƒœ๊ทธ์— ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๋ณด๋‹ˆ ์ด์ „์— PHP๋กœ ํšŒ์›๊ฐ€์ž…์„ ๋งŒ๋“ค์—ˆ๋˜ ํ™”๋ฉด์ด ๋– ์˜ฌ๋ž๋‹ค.

ํšŒ์›๊ฐ€์ž… ํ›„ ํšŒ์›์ •๋ณด์ˆ˜์ •์— ๋“ค์–ด๊ฐ€๋ฉด ํšŒ์› ์•„์ด๋””๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ •๋ณด๋ฅผ ์ฐพ์•„ value์— ๋ฟŒ๋ ค์คฌ์—ˆ๋‹ค.

 

์ด๊ฒƒ๋„ ๊ทธ๊ฒƒ๊ณผ ๊ฐ™์ง€ ์•Š์„๊นŒ..

 

 

Input ํƒœ๊ทธ์— title ๊ฐ’ ๋ฟŒ๋ฆฌ๊ธฐ

 

 

์šฐ์„  ํด๋ฆญํ•˜๋ฉด editId๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” Input์— ๋ฟŒ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณด์ž.

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— editToGoalForm ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” state๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ App.js ์— ์žˆ์œผ๋ฉด ๋˜๊ฒ ๋‹ค.

 

 

//๋ชฉํ‘œ GoalForm์œผ๋กœ ๋ณด๋‚ด๊ธฐ ํ•จ์ˆ˜
function editToGoalForm(id){
	const thisItems = goal.items; // goal State ์›๋ณธ ์นดํ”ผ

}

 

 

์ธ์ž๋กœ ๋“ค์–ด์˜จ id๋ฅผ ์ด์šฉํ•ด ํ•ด๋‹น state๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

ํ˜„์žฌ state์˜ ์›๋ณธ์„ ์นดํ”ผํ•˜๊ธฐ ์œ„ํ•ด thisItems ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด์ค€๋‹ค. ๋Š” ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ id๊ฐ’์ด ์ฃผ์–ด์ง„ ์ธ์ž์™€ ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋Š”์ง€ ์ฐพ์•„๋ณด์ž.

 

thisItems๋ฅผ ์ฝ˜์†”๋กœ ์ฐ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

 

๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๋ฐ˜๋ณต๋ฌธ์„ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ์กฐ๊ฑด์„ ์“ธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— deleteGoal ์ฒ˜๋Ÿผ filter ํ•จ์ˆ˜๋ฅผ ์จ๋ด์•ผ๊ฒ ๋‹ค.

 

 

 

   //๋ชฉํ‘œ GoalForm์œผ๋กœ ๋ณด๋‚ด๊ธฐ ํ•จ์ˆ˜
   function editToGoalForm(id){
    const thisItems = goal.items; // goal State ์›๋ณธ ์นดํ”ผ
    const editItems = thisItems.filter(e => e.id === id)
    console.log(editItems)
  }

 

 

์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ฐ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

 

 

 

๊ทธ๋Ÿผ ์ด์ œ ์ด ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  editItems๋ผ๋Š” ๋ฐฐ์—ด์„ Input์— ์ „๋‹ฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์•„์ง๋„ ์ „๋‹ฌ์„ ๋ชปํ–ˆ๋‹ค.

 

๋งŒ๋“  editItems๋ฅผ GoalForm์— ์ „๋‹ฌํ•ด์ฃผ๊ณ  ์‹ถ์€๋ฐ...

Goals์—๋‹ค ๋˜ ์ „๋‹ฌํ•˜๊ณ  GoalForm์œผ๋กœ ๋˜ ๊ฐ€์•ผํ• ๊นŒ?

๊ทธ๋ƒฅ App.js์— ์žˆ๋Š” Goals ์ปดํฌ๋„ŒํŠธ ์œ„์— ๋„ฃ์–ด๋„ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ.. ํ•จ์ˆ˜๋ฅผ ๋ณด๋‚ด๊ธฐ๋„ ๋” ํŽธํ•˜๊ณ .

 

<Route exact path="/goals">
        <BasicNavBar/>
          <GoalForm addGoal={addGoal} editTmpGoal={editTmpGoal}  />
          <Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal}
          editToGoalForm={editToGoalForm}  />
        </Route>

 

Goals ์ปดํฌ๋„ŒํŠธ์—์„œ GoalsForm ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋นผ๊ณ  App.js ์— ๋ผ์šฐํ„ฐ ๋‚ด์˜ ๋ฐฐ์น˜๋ฅผ ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ์—ˆ๋‹ค.

 

 

 

์‚ฌ์‹ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋งŒ๋“ค๋ผ๊ณ  ํ•˜๋ฉด ๊ธˆ๋ฐฉ ๋งŒ๋“ค๊ฒ ๋Š”๋ฐ ๋ฆฌ์•กํŠธ๋กœ ํ•˜๋‹ˆ๊นŒ ์ปดํฌ๋„ŒํŠธ์˜ ์ˆœ์„œ๋„ ๋งž์ถฐ์•ผํ•˜๊ณ , props๋„ ๋‚ด๋ ค์•ผ ํ•˜๊ณ  ... ๊ณ ๋ฏผ์ด ์ด๋งŒ์ €๋งŒ์ด ์•„๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ์—๋Š” ์™œ ๋ฆฌ์•กํŠธ๋ฅผ ์“ฐ๊ฒŒ ๋˜๋Š”์ง€ ์•Œ๊ฒŒ ๋œ๋‹ค๋Š”๋ฐ..

 

์ง€๊ธˆ ํ•ด์•ผํ•  ์ผ์€ ๋ชฉํ‘œ ํด๋ฆญํ•˜๋ฉด! textfield์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

๋‹น์—ฐํžˆ DOM์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—  textfield๋ฅผ ์ฟผ๋ฆฌ์…€๋ ‰ํ„ฐ๊ฐ™์€๊ฑธ๋กœ ์„ ํƒํ•ด์•ผํ•˜๋‚˜? ํ–ˆ๋”๋‹ˆ

๋˜ ํ›…์ด ์žˆ๋‹ค.

 

ํ›…..

 

๊ทธ๋ฆฌ๊ณ  ์ˆ˜์ • ์ƒํƒœ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ, textfield์— value๊ฐ€ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ

๊ทธ๊ฒƒ๋ณด๋‹ค๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ ์ด๊ณ , ์ €๋ฒˆ์— ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ readOnly ๊ฐ€ false์ผ ๋•Œ, ์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ธฐ์กด GoalForm ์ปดํฌ๋„ŒํŠธ ์œ„์— ๋ฎ์ด๋Š”๊ฒŒ ๋‚ซ์ง€ ์•Š์„๊นŒ?

 

 

์šฐ์„  useRef ๋จผ์ € ์จ๋ด์•ผ๊ฒ ๋‹ค.

 

 

useRef ์‚ฌ์šฉํ•˜๊ธฐ

 

 

useRef ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

App.js ์— ๋งŒ๋“ค์–ด๋ณด์ž.

 

 

const goalform_textfield = useRef;

 

 

๊ทธ๋ฆฌ๊ณ  ๋ณ€๊ฒฝ์„ ์œ„ํ•˜๋Š” ํƒœ๊ทธ์— ์†์„ฑ์œผ๋กœ ref ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปดํฌ๋„ŒํŠธ์— props๋กœ ref๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๋‚ด๋ ค์ค˜์•ผํ•œ๋‹ค. ๋‚˜๋Š” TextField์˜ ๊ฐ’(value)๊ฐ€ ๋ณ€๊ฒฝ๋˜์•ผ ํ•˜๋Š”๋ฐ, ํด๋ฆญ ์ด๋ฒคํŠธ๋Š” Goals์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— GoalForm๊ณผ Goals ๋ชจ๋‘ ref props๋ฅผ ๋‚ด๋ ค์ค€๋‹ค. ref ๋ผ๋Š” ์†์„ฑ์€ ๋‚˜์ค‘์— ์“ธ๊ฑฐ๊ธฐ ๋•Œ๋ฌธ์— TextField_Ref์ด๋ผ๊ณ  ๋‚ด๋ ค์ค€๋‹ค.

 

 

  <GoalForm addGoal={addGoal} editTmpGoal={editTmpGoal} TextField_Ref={TextField_Ref} />
  <Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} 
  editToGoalForm={editToGoalForm} TextField_Ref={TextField_Ref}   />

 

 

๊ทธ๋ฆฌ๊ณ  GoalForm ์ปดํฌ๋„ŒํŠธ์˜ TextField์— props๋กœ ๋ฐ›์•„์˜จ ref๋ฅผ ๋„ฃ์–ด์ฃผ์ž.

 

 

<TextField fullWidth id="goalform_textfield" variant="standard"
onChange={onInputChange} ref={ TextField_Ref } />

 

 

๊ทธ๋ฆฌ๊ณ  ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด์„œ editToGoalForm ํ•จ์ˆ˜์— goalField.current.focus()๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

.focus๋Š” ๊ทธ ๋ถ€๋ถ„์— ํฌ์ปค์Šค๋ฅผ ์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋‹ค.

 

ํ .. ์•„๋ฌด๋Ÿฐ ๋ฐ˜์‘์ด ์—†์—ˆ๋‹ค. Ref ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ‹€๋ฆฐ๊ฒŒ ์•„๋‹๊นŒ?

 

 

Ref๋ฅผ ์ค˜์•ผํ•˜๋Š” ๊ณณ์€ GoalForm์ด๊ณ , ์‹ค์ œ ์ด๋ฒคํŠธ๊ฐ€ ์‹œ์ž‘๋˜๋Š” ๊ณณ์€ Goals ์ด๋‹ค.

์›๋ž˜ Goals ์•ˆ์— GoalForm์ด ์žˆ์—ˆ์œผ๋‚˜ ๋‘๊ฐœ๋ฅผ ๋‚˜๋ž€ํžˆ ๋ณ‘๋ ฌ๋ฐฐ์น˜ํ–ˆ๋Š”๋ฐ..

 

 

๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜์ž๋ฉด ์ด๋ ‡๋‹ค.

 

 

 

 

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Goals์™€ GoalForm์ด Ref๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ.. ref๋กœ ์ง€์ •ํ•œ ํƒœ๊ทธ๊ฐ€ GoalForm์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Goals์—์„œ ์“ฐ์ง€ ๋ชปํ•˜๋Š”๊ฒŒ ์•„๋‹๊นŒ? ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ํ”Œ๋žœ B๋กœ ๋งŒ๋“ค์–ด ๋ณผ ์˜ˆ์ •..

 

 

์ด๊ฒƒ๋งŒ ํ•ด๊ฒฐํ•˜๋ฉด ๋‹ค์Œ ์ง„๋„๊ฐ€ ์ข€ ์ž˜ ๋‚˜๊ฐˆ๊ฑฐ๊ฐ™์€๋ฐ ๊ฑฐ์˜ ์ผ์ฃผ์ผ ๋™์•ˆ ํด๋ฆญ->TextField์— ๊ฐ’ ์˜ฌ๋ฆฌ๊ธฐ ๋ฅผ ํ—ค๋ฉ”๊ณ  ์žˆ๋‹ค ใ… ใ… 

 

 

 

728x90