[React] A component is changing an uncontrolled input to be controlled.
728x90

์–ด๋–ค ์—๋Ÿฌ?

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๋‚ด๋ ค์ค€ props๋ฅผ ๋ฟŒ๋ ค์ฃผ๋‹ค๊ฐ€ input ๊ด€๋ จ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.

 

 

์›์ธ ํŒŒ์•…

์œ„์˜ ์ฝ˜์†”๋“ค์„ ๋ณด๋‹ˆ ํŽ˜์ด์ง€ ์ง„์ž… ์‹œ์—๋Š” ๊ดœ์ฐฎ์€๋ฐ, ์ƒˆ๋กœ๊ณ ์นจ ํ–ˆ์„ ๋•Œ uid๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์‹œ๊ฐ„์ด ์žˆ์–ด userData๊ฐ€ ์ž ๊น ๋น„์–ด๋ฒ„๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ์กด์žฌํ•œ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ๊ฐ’์ด ์—†์–ด์„œ undefined๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด ์›์ธ์ธ๋“ฏ ํ•˜๋‹ค!..

 

 

ํ•ด๊ฒฐ

์—ฌ๋Ÿฌ ๊ธ€๋“ค์„ ์ฐธ๊ณ ํ•ด ๋ณด๋‹ˆ input ํƒœ๊ทธ์˜ value๊ฐ€ undefined์ผ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋“ฏํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋ณดํ†ต ์•„๋ž˜์™€ ๊ฐ™์ด OR ์—ฐ์‚ฐ์ž๋กœ ๋นˆ๊ฐ’์„ ์ฃผ๊ฑฐ๋‚˜...

value = {value || ''}

defaultValue๋ฅผ ์„ค์ •ํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋“ฏํ–ˆ๋‹ค. ๋Œ€๋‹ค์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐ๋œ๋‹ค.

 

 

 

ํ•˜์ง€๋งŒ? ๋‚˜๋Š” inputํƒ€์ž…์ด text๊ฐ€ ์•„๋‹ˆ๋ผ radio์ธ ์ƒํ™ฉ...
์•„๋ž˜๋Š” ๊ธฐ์กด์— ์ง  ์ฝ”๋“œ. ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ true์ธ ๊ฒฝ์šฐ์—๋งŒ checked๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค.

{career.map((career) => (
     <CareerRadioBox key={career.id}>
          {isJunior ? (
            <InfoRadioBoxInput
              type="radio"
              id={career.id}
              name="isJunior"
              checked
            />
          ) : (
        <InfoRadioBoxInput type="radio" id={career.id} name="isJunior" />
)}

์œ„์—์„œ defaultValue ์„ค์ •์œผ๋กœ๋„ ํ•ด๊ฒฐ์ด ๋œ๋‹ค๋Š” ๊ธ€์„ ๋ณด์•˜์œผ๋‹ˆ ๋ญ”๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ํ–ˆ๋Š”๋ฐ?

 


๊ตฌ๊ธ€๋ง ํ•˜๋‹ค๊ฐ€ ๊ฑฐ์˜ ๋™์ผํ•œ ์ด์Šˆ๋ฅผ ์ฐพ์•„๋ƒˆ๋‹ค!.. ๋ฌด๋ ค ๋ฆฌ์•กํŠธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜.. ์ด์Šˆ๊ธ€.. ๐Ÿ‘€

์•„๋ž˜ ๋‹ต๋ณ€์„ ์ฐธ๊ณ ํ•˜์—ฌ defaultChecked๋ฅผ ๋„ฃ์œผ๋‹ˆ ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋‹ค!

 

 

์ˆ˜์ •ํ•œ ์ฝ”๋“œ

{career.map((career) => (
        <CareerRadioBox key={career.id}>
          {isJunior ? (
            <InfoRadioBoxInput
              type="radio"
              id={career.id}
              name="isJunior"
              defaultValue={career.value}
              defaultChecked
            />
          ) : (
            <InfoRadioBoxInput
              type="radio"
              id={career.id}
              defaultValue={career.value}
              name="isJunior"
            />
          )}

 

 

 

ํ•ด๊ฒฐ ์™„๋ฃŒ!

์ƒˆ๋กœ๊ณ ์นจ ์‹œ ์ด๋ฏธ์ง€ ๋ฐ›์•„์˜ค๋Š” ๊ฒŒ ์กฐ๊ธˆ ๋Š๋ฆฐ๋ฐ ์ด๊ฑด ์—๋Ÿฌ๋ž‘์€ ๋ณ„๊ฐœ๋‹ˆ ์ถ”ํ›„ ํ•ด๊ฒฐํ•ด ๋ด์•ผ์ง€ ๐Ÿ‘€

 

 

์ฐธ๊ณ ๊ธ€

https://github.com/facebook/react/issues/6779

728x90