ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋งˆ์ดํŽ˜์ด์ง€ ์ˆ˜์ • ๊ธฐ๋Šฅ์„ ์ง„ํ–‰ ์ค‘์ด๋‹ค. ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋‹‰๋„ค์ž„ ์ˆ˜์ •ํ•˜๊ธฐ์ด๋‹ค.

 

React Hook Form ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” useState ๋กœ ๋‹‰๋„ค์ž„ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜์˜€๊ณ  DB ์—์„œ ์กฐํšŒํ•œ ์ •๋ณด๋กœ setNickname() ํ•ด์ฃผ๋ฉด ๋๋Š”๋ฐ.. react hook form ์€..? ์ด ์‚ฌํ•ญ์„ ๋†“์น˜๊ณ  ์žˆ์–ด์„œ ์ˆ˜์ •ํŽ˜์ด์ง€ ๊ฐ€๋ฉด DB ์—์„œ ๊ฐ€์ ธ์˜จ ์œ ์ € ์ •๋ณด๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. ๋ณดํ†ต ์ˆ˜์ •ํŽ˜์ด์ง€ ๊ฐ€๋ฉด ์›๋ž˜ ์„ค์ •ํ•ด ๋†“์€ ์ •๋ณด๊ฐ€ ๋œจ๊ธฐ ๋งˆ๋ จ์ด๋‹ˆ.. ๋‚˜๋Š” ๊ธฐ์กด ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•ด ์ค„ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค..!

 

์ด์Šˆ

React Hook form ์„ ์‚ฌ์šฉํ•  ๋•Œ DB ์— ์žˆ๋Š” ๊ธฐ์กด ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•˜๋Š”๊ฐ€? ํ˜„์žฌ๋Š” ์ˆ˜์ • ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ธฐ์กด์˜ ๋‹‰๋„ค์ž„์ด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.

 

ํ•ด๊ฒฐ

์ด๊ฒƒ์ €๊ฒƒ ์ฐพ์•„๋ณด๋‹ˆ.. setValue ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ฐพ์Œ ๐Ÿ˜ฎ‍๐Ÿ’จ

const ProfileUpdate = ({ userId }: { userId: string }) => {
  const {
    register,
    handleSubmit,
    formState: { errors },
    setValue,
  } = useProfileForm();

.. ์ƒ๋žต

  const { data: profileData, isLoading: isProfileLoading, isError: isProfileError } = useProfilesQuery(userId);

  useEffect(() => {
    if (profileData && profileData.profiles?.user_nickname) {
      const userNickname = profileData.profiles.user_nickname;
      setValue('nickname', userNickname); // nickname ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
      setCount(userNickname.length); 
    }
  }, [profileData, setValue]);

... ์ƒ๋žต

setValue(๋ณ€์ˆ˜, ๊ฐ’) ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

๊ฒฐ๊ณผ

๋งˆ์ดํŽ˜์ด์ง€ ๋ฉ”์ธ์—์„œ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ˆ˜์ • ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ ํ›„, '๋ฐ๋ฐ' ์ด๋ผ๋Š” DB ์—์„œ ์กฐํšŒ๋œ ์œ ์ € ์ •๋ณด๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

์ฐธ๊ณ  ์ž๋ฃŒ

https://react-hook-form.com/docs/useform/setvalue

 

useForm - setValue

Performant, flexible and extensible forms with easy-to-use validation.

react-hook-form.com

 

์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
Total
Today
Yesterday
๋งํฌ