Record4me

์‹œ์ž‘ํ•˜๋ฉด ๋์„ ๋ด์•ผ์ง€

TIL

[TIL] 24_0308 input๊ฐ’ ํƒ€์ž…์— ๋Œ€ํ•œ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

์ž‡์ธ„๋ฏธ 2024. 3. 8. 18:44

 

๐Ÿ’ฅํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…๐Ÿ’ฅ

๐Ÿ“Œ ๊ด€๋ จ ์ด์Šˆ

 

input๊ฐ’ ํƒ€์ž…์— ๋Œ€ํ•œ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ… ํ•ด๊ฒฐ

 

-๋ฌธ์ œ : ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ ํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ๊ณต๋ฐฑ์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•ด์„œ ๋นˆ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•˜๋ ค๊ณ  replaceํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ,  

{}' ํ˜•์‹์— 'replace' ์†์„ฑ์ด ์—†์Šต๋‹ˆ๋‹ค."



๋ผ๋Š” ๋ฉ”์„ธ์ง€๊ฐ€ ๋œธ.

- ์›์ธ : useInput์˜ initialState์˜ interfaceํƒ€์ž…์„ any์—์„œ unknown์œผ๋กœ ๋ฐ”๊ฟจ์„ ๋•Œ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ interface์—์„œ ์ง€์ •ํ•œ stringํƒ€์ž…์„ ์ธ์ง€ ๋ชป ํ•จ.


- ๋ฐฉ์•ˆ1 : as  string์œผ๋กœ ํƒ€์ž…์„ ํ™•์ •ํ•ด์คŒ.
- ๋ฐฉ์•ˆ2 : ๋งค๊ฐœ๋ณ€์ˆ˜์—๋งŒ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜ค๋Š” initialState์˜ ๊ฐ’์— ์ œ๋„ค๋ฆญ์œผ๋กœ ํƒ€์ž…์ง€์ •ํ•ด ์ฃผ๊ธฐ.  
- ๋ฐฉ์•ˆ3 : ์‚ฌ์šฉ๋œ useInput ์ปค์Šคํ…€ ํ›…์˜ initialState์˜ ํƒ€์ž…์„ ์ œ๋„ค๋ฆญ์œผ๋กœ ํƒ€์ž…์„ ๋™์ ์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๋ฆฌํ„ด ๊ฐ’์„ ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•˜๊ธฐ.


- ์„ ํƒํ•œ ๋ฐฉ์•ˆ : ๋ฐฉ์•ˆ 1, ๋ฐฉ์•ˆ 2
- ์„ ํƒํ•œ ์ด์œ  : ๋ฐฉ์•ˆ 1์ด ํ˜„์žฌ  ๋ฐฉ์•ˆ 2๋ฅผ ๊ณ ์ˆ˜ํ•˜๊ธฐ์—” ์•„์ง ์ œ๋„ค๋ฆญ์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ์žกํ˜€์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ œ๋„ค๋ฆญ์„ ์ข€ ๋” ๊ณต๋ถ€ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ.


- ํ•ด๊ฒฐ 1 : as string์„ ๋ถ™์—ฌ์ฃผ์–ด ๊ฐ’์˜ ํƒ€์ž…์„ ํ™•์ •ํ•จ. 
(as ํƒ€์ž… ํ˜•ํƒœ๋Š” ์‹ค์ƒ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์ž ๋ณธ์ธ์ด ๊ฐ•์ œํ•ด์„œ ํƒ€์ž…์„ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ ์ตœ์ข…์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์จ์•ผํ•จ.)
- ํ•ด๊ฒฐ 2 : ๋ฐฉ์•ˆ 2์„ ์‚ฌ์šฉํ•ด์„œ useInput์—์„œ ์˜ค๋ฅ˜์—๋งŒ ํ•ด๋‹นํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ’์œผ๋กœ ๋“ค์–ด์˜ค๋Š” initialState๊ฐ’๋งŒ ์ œ๋„ค๋ฆญ์œผ๋กœ ํƒ€์ž…์ง€์ •ํ•ด์คŒ.
- ๋ฌธ์ œ์™€ ๊ด€๋ จ ์ฝ”๋“œ
- useInput.tsx

interface initialFormType {
  [key: string]: unknown;
}



-FormTodo.tsx

  const [todoInput, setTodoInput, onChange, reset] = useInput(init);
  const refTitle = useRef<HTMLInputElement>(null);
  const titleInput = (todoInput?.title || "") as string;
  const commentInput = (todoInput?.comment || "") as string;
  const deadLineInput = (todoInput?.deadLine || "") as string;
  const blankPattern = /^\s+|\s+$/g;

~ ๋‚ด์šฉ ์ƒ๋žต~

  // ๋“ค์–ด์˜จ ๊ฐ’์œผ๋กœ ๊ต์ฒดํ•ด์ฃผ๊ธฐ
  const onSubmitHand = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    const titleBlank = (titleInput || "").replace(blankPattern, "");
    const commentBlank = (commentInput || "").replace(blankPattern, "");



ํ•ด๊ฒฐ 1 :  as string์„ ๋ถ™์—ฌ์ฃผ์–ด ๊ฐ’์˜ ํƒ€์ž…์„ ํ™•์ •ํ•จ. 

-ํ•ด๊ฒฐ ์ „ FormTodo.tsx

const titleInput = todoInput?.title || "";
 const commentInput = todoInput?.comment || "";
 const deadLineInput = todoInput?.deadLine || "";



-ํ•ด๊ฒฐ ํ›„ FormTodo.tsx

 const titleInput = (todoInput?.title || "") as string;
  const commentInput = (todoInput?.comment || "") as string;
  const deadLineInput = (todoInput?.deadLine || "") as string;




-ํ•ด๊ฒฐ 2 :  ๋ฐฉ์•ˆ 2์„ ์‚ฌ์šฉํ•ด์„œ useInput์—์„œ ์˜ค๋ฅ˜์—๋งŒ ํ•ด๋‹นํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ’์œผ๋กœ ๋“ค์–ด์˜ค๋Š” initialState๊ฐ’๋งŒ ์ œ๋„ค๋ฆญ์œผ๋กœ ํƒ€์ž…์ง€์ •ํ•ด์คŒ.

-๋ฌธ์ œ์˜ useInput.tsx

import { ChangeEvent, useCallback, useState } from "react";

interface initialFormType {
  [key: string]: any;
}

const useInput = (initialForm: initialFormType) => {
  const [form, setForm] = useState<initialFormType>(initialForm);

const onChange = useCallback(
   (
      e:
        | ChangeEvent<HTMLInputElement>
        | ChangeEvent<HTMLSelectElement>
        | ChangeEvent<HTMLTextAreaElement>
    ) => {
      const { name, value } = e.target;
      setForm((preForm) => ({ ...preForm, [name]: value }));
    },
    []
 );
  const reset = useCallback(() => setForm(initialForm), [initialForm]);

return [form, setForm, onChange, reset] as const;
};

export default useInput;




- ํ•ด๊ฒฐ ํ›„
- useInput.tsx

import { ChangeEvent, useCallback, useState } from "react";

interface initialFormType {
  [key: string]: unknown;
}

// ์ œ๋„ค๋ฆญ์œผ๋กœ ๊ฐœ์„  => ํƒ€์ž…์„ ๋™์ ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.
const useInput = <T extends initialFormType>(initialForm: T) => {
  const [form, setForm] = useState<T>(initialForm);

  //onChange
  const onChange = useCallback(
    (
      e:
        | ChangeEvent<HTMLInputElement>
        | ChangeEvent<HTMLSelectElement>
        | ChangeEvent<HTMLTextAreaElement>
    ) => {
      const { name, value } = e.target;
      setForm((preForm) => ({ ...preForm, [name]: value }));
    },
    []
  );
  const reset = useCallback(() => setForm(initialForm), [initialForm]);

  return [form, setForm, onChange, reset] as const; // ๊ฐ์ฒด๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒŒ ์ข‹๋‹ค
};

export default useInput;




- FormTodo.tsx

 const titleInput = todoInput?.title || "";
 const commentInput = todoInput?.comment || "";
 const deadLineInput = todoInput?.deadLine || "";