โœ๏ธ What I Learned/TIL

[TIL] ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” data์— ์ œ๋„ค๋ฆญ ์‚ฌ์šฉํ•˜๊ธฐ

Jiwon() 2023. 9. 14. 10:24

๐Ÿ’ก TIL 20230914 - ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” data์— ์ œ๋„ค๋ฆญ ์‚ฌ์šฉํ•˜๊ธฐ


๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” data์— ํƒ€์ž… ์—๋Ÿฌ ๋ฐœ์ƒ

useMutation.tsx์—์„œ useMutation ํ›…์„ ํ†ตํ•ด ๋ฐ›์•„์˜ค๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ง€์ •ํ•ด๋‘์—ˆ๋‹ค.

// src/hooks/useMutation.tsx

interface UseMutationState<T> {
  loading: boolean;
  data?: object;
  error?: object;
}

 

// src/components/Auth/Auth.tsx

// ์ƒ๋žต
  return (
    // ... ์ค‘๊ฐ„ ์ƒ๋žต
          {data?.ok ? null : (
            <form onSubmit={handleSubmit(onValid)} className="flex flex-col mt-8">
            <div className="mt-1">
              {method === "email" ? (
                <>

๊ทธ๋žฌ๋”๋‹ˆ ์œ„์˜ data?.ok ๋ถ€๋ถ„์—์„œ 'Property 'ok' does not exist on type 'object'. ts(2339)' ํƒ€์ž… ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

useMutation์„ ํ†ตํ•ด ๋ฐ›์•„์˜ค๋Š” data๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์„ ์–ธ ์‹œ์ ์— ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํŒ๋ณ„ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๋”ฐ๋ผ์„œ data๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ (๋น„๋™๊ธฐ๋ฅผ ํ†ตํ•ด data๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์‹œ์ )์— ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด์„œ ํƒ€์ž…์„ ์ง€์ •ํ•˜์˜€๋‹ค.

 


์ œ๋„ค๋ฆญ ํƒ€์ž…(Generic Type)์ด๋ž€?

์„ ์–ธ ์‹œ์ ์ด ์•„๋‹ˆ๋ผ ์ƒ์„ฑ ์‹œ์ ์— ํƒ€์ž…์„ ๋ช…์‹œํ•˜์—ฌ ํ•˜๋‚˜์˜ ํƒ€์ž…๋งŒ์ด ์•„๋‹Œ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋ฒ•

  • ํ•œ๋ฒˆ์˜ ์„ ์–ธ์œผ๋กœ ๋‹ค์–‘ํ•œ ํƒ€์ž…์— ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
  • T๋Š” ์ œ๋„ค๋ฆญ์„ ์„ ์–ธํ•  ๋•Œ ๊ด€์šฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์‹๋ณ„์ž๋กœ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ(Type parameter)๋ผ ํ•œ๋‹ค.
  • ํ•จ์ˆ˜์—๋„ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•˜๋‚˜์˜ ํƒ€์ž…๋งŒ์ด ์•„๋‹Œ ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฆฌํ„ด๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

useMutation.tsx

import { useState } from "react";

interface UseMutationState<T> {
  loading: boolean;
  data?: T;
  error?: object;
}

type UseMutationResult<T> = [(data: any) => void, UseMutationState<T>];

const useMutation = <T = any>(url: string): UseMutationResult<T> => {
  const [state, setState] = useState<UseMutationState<T>>({
    loading: false,
    data: undefined,
    error: undefined,
  })
  const mutationFn = async (data: any) => {
    fetch(url, {
      method: "POST",
      body: JSON.stringify(data),
    })
      .then((res) => res.json().catch(() => {}))
      .then((data) => setState((prev) => ({ ...prev, data, loading: false })))
      .catch((error) => setState((prev) => ({ ...prev, error })))
      .finally(() => setState((prev) => ({ ...prev, loading: false })))
  };
  return [mutationFn, { ...state }];
};

export default useMutation;
  • <T = any> : ์šฐ์„  useMutation.tsx์—์„œ useMutation์— ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ any ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค๊ณ  ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.
  • UseMutationResult<T> : ์ด๊ฑธ UseMutationResult๋กœ ๋ณด๋‚ด๊ณ ,
  • UseMutationState<T> : UseMutationResult๊ฐ€ ๋ฐ›์œผ๋ฉด UseMutationState์—๋„ ๋ณด๋‚ด์ค€๋‹ค.
  • data?: T : UseMutationState๊ฐ€ ๋ฐ›์œผ๋ฉด data?: ๋„ Tํƒ€์ž…์ด๋ผ๊ณ  ๋ช…์‹œํ•ด์ค€๋‹ค.

 

์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•œ Auth.tsx ๊ฒฐ๊ณผ

// components/Auth.tsx

"use client";
import { NextComponentType } from "next";
import { useState } from "react";

import { useForm } from "react-hook-form";
import { useMutation, useIsMobile } from "@/hooks";

import { TabButtons, Button, Input } from "@/components";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faComment } from "@fortawesome/free-solid-svg-icons";

interface AuthForm {
  email?: string;
  phone?: string;
}

// AuthMutationResult ์ถ”๊ฐ€
interface AuthMutationResult {
  ok: boolean;
}

const Auth: NextComponentType = () => {
	// ... ์ค‘๊ฐ„ ์ƒ๋žต
    
  return (
    // ... ์ค‘๊ฐ„ ์ƒ๋žต
        <div className="max-w-2xl mx-auto">
          {data?.ok ? null : (
            <form onSubmit={handleSubmit(onValid)} className="flex flex-col mt-8">

์œ„์™€ ๊ฐ™์ด data์˜ ํƒ€์ž…์ด AuthMutationResult ๋˜๋Š” undefined๋กœ ํƒ€์ž…์ด ์ง€์ •๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


์ฐธ๊ณ  ์‚ฌ์ดํŠธ

๋…ธ๋งˆ๋“œ ์ฝ”๋” - ์บ๋Ÿฟ๋งˆ์ผ“ ํด๋ก ์ฝ”๋”ฉ

12.7 TypeScript - Generic | PoiemaWeb