๐ฅํธ๋ฌ๋ธ ์ํ ๐ฅ
๐ ๊ด๋ จ ์ด์
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 || "";
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] 24_0528 vite, vue ํํ ๋ฆฌ์ผ (0) | 2024.05.28 |
---|---|
[TIL] 24_0305 ํ์ ์คํฌ๋ฆฝํธ(TypeScript)์ ์ปดํ์ผ๋ฌ (0) | 2024.03.05 |
[TIL] 24_0304 ์๋ฐ์คํฌ๋ฆฝํธ์ ์ฝ์ (0) | 2024.03.04 |
[TIL] 24_0229 ๋ค์ฏ๋ฒ์งธ ํ๋ก์ ํธ์ ๋ํ ํ๊ณ (0) | 2024.02.29 |
[TIL] 24_0221 ๋ฆฌ๋์ค thunk, tookit ์์์ ์คํ, ๋ฆฌ๋์ค์ vscord์ ๊ธฐ๋ฅ๊ณผ ์ด๋ฆ ์ถฉ๋๋ก ์ธํ ์ค๋ฅ (0) | 2024.02.21 |