TypeScript: React.FC types

55555-JyeonΒ·2024λ…„ 2μ›” 6일
0

What's

λͺ©λ‘ 보기
6/7
post-thumbnail

🧐 μ‹€λ¬΄μ—μ„œ 자주 μ‚¬μš©ν•˜λŠ” νƒ€μž… λ°”λ‘œ μ•ŒκΈ° (3/3)

νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—λŠ” ꡉμž₯히 λ§Žμ€ νƒ€μž…λ“€μ΄ μ‘΄μž¬ν•˜κ³  μ‹€μ œλ‘œλŠ” 자주 μ‚¬μš©λ˜μ§€ μ•ŠλŠ” νƒ€μž…λ“€λ„ 많이 μ‘΄μž¬ν•©λ‹ˆλ‹€.
μ•„λž˜λŠ” λ§Žμ€ κ°œλ°œμžλ“€μ΄ μ‹€λ¬΄μ—μ„œ μ‚¬μš©ν•˜λ©΄μ„œ κ°„ν˜Ή ν˜Ήμ€ 자주 μ‚¬μš©ν•˜λŠ” νƒ€μž…λ“€ 쀑 λ¦¬μ•‘νŠΈ νƒ€μž…λ“€μž…λ‹ˆλ‹€.




ν‘œλ‘œ μ •λ¦¬ν•œ λ¦¬μ•‘νŠΈ νƒ€μž…


React Types
type desc.
FC
ReactNode children μ†μ„±μ˜ νƒ€μž…μœΌλ‘œ κ°€μž₯ 많이 μ‚¬μš©ν•˜λŠ” νƒ€μž…
ReactElement createElement ν•¨μˆ˜λ₯Ό 톡해 μƒμ„±λœ μ»΄ν¬λ„ŒνŠΈλ§Œμ„ ν—ˆμš©ν•˜λŠ” νƒ€μž…
PropsWithChildren
PropsWithRef 일반 React.FCμ—μ„œ Refλ₯Ό prop으둜 μ‚¬μš©
RefObject
SetStateAction setState λ©”μ†Œλ“œμ˜ κ²½μš°μ—λŠ” react 라이브러리의 Dispatch, SetStateAction λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©
Dispatch
HTML___Element


πŸ” μ˜ˆμ‹œλ‘œ 더 μžμ„Ένžˆ μ‚΄νŽ΄λ³΄λŠ” κΈ°λ³Έ νƒ€μž…

react 18버전 μ΄μ „κΉŒμ§€ FC μ‚¬μš©μ„ μ§€μ–‘ν–ˆλ˜ μ΄μœ μ™€ 이제 λ‹€μ‹œ μ‚¬μš©ν•  수 μžˆλŠ” μ΄μœ λŠ” λ¬΄μ—‡μΌκΉŒ?
λ§Œμ•½ FCλ₯Ό μ‚¬μš©ν•  수 μ—†λŠ” ν™˜κ²½μ΄λΌλ©΄ μ΄μœ λŠ” 무엇이고 μ–΄λ–»κ²Œ λŒ€μ²˜κ°€ κ°€λŠ₯ν•œκ°€?

React.FC

React.FCλŠ” Reactμ—μ„œ μ‚¬μš©λ˜λŠ” νƒ€μž…μŠ€ν¬λ¦½νŠΈ μš©μ–΄λ‘œ, "Function Component"의 μ•½μžμ΄λ‹€.
ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈ(FC)λŠ” μ΄ˆκΈ°μ— μƒνƒœλ‚˜ 라이프사이클 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜κΈ° μ–΄λ €μ› κΈ° λ•Œλ¬Έμ—, 더 λ§Žμ€ κΈ°λŠ₯이 ν•„μš”ν•œ 경우 ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μΌλ°˜μ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.
λ¦¬μ•‘νŠΈ 16.8λΆ€ν„° Hooksκ°€ λ„μž…λ˜λ©΄μ„œ ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈ(FC)μ—μ„œλ„ μƒνƒœ 및 라이프사이클을 효과적으둜 관리할 수 있게 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈ(FC)λ₯Ό μ‚¬μš©ν•  수 μ—†λŠ” ν™˜κ²½μ΄λΌλ©΄ ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•΄μ„œ 클래슀의 νŠΉμ„±μ„ ν™œμš©ν•΄ λ§Žμ€ κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ 라이프사이클 λ©”μ„œλ“œλ₯Ό ν™œμš©ν•΄ μ»΄ν¬λ„ŒνŠΈμ˜ μƒνƒœ 변화에 따라 λ™μž‘μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

import { ReactNode, FC } from "react";

type PostProps = {
  title: string;
  content: ReactNode;
};

export const Posts = ({ title, content }: PostProps) => {
  return (
    <div>
      {title}
      {content}
    </div>
  );
};

ReactNode

children μ†μ„±μ˜ νƒ€μž…μœΌλ‘œ κ°€μž₯ 많이 μ‚¬μš©ν•˜λŠ” νƒ€μž… πŸ‘‰ jsx λ‚΄μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” λͺ¨λ“  μš”μ†Œμ˜ νƒ€μž… (string, null, undefined 등을 ν¬ν•¨ν•˜λŠ” κ°€μž₯ 넓은 λ²”μœ„λ₯Ό κ°–λŠ” νƒ€μž…)
μ–΄λ–€ props을 받을 건데, ꡬ체적으둜 μ–΄λ–€ νƒ€μž…μ΄ μ˜¬μ§€ μ•Œ 수 μ—†κ±°λ‚˜, μ–΄λ– ν•œ νƒ€μž…λ„ λͺ¨λ‘ λ°›κ³  μ‹Άλ‹€λ©΄ ReactNode둜 μ§€μ •ν•΄μ£ΌλŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” renderλ©”μ†Œλ“œμ—μ„œ ReactNodeλ₯Ό 리턴


ReactElement

createElement ν•¨μˆ˜λ₯Ό 톡해 μƒμ„±λœ μ»΄ν¬λ„ŒνŠΈλ§Œμ„ ν—ˆμš©ν•˜λŠ” νƒ€μž…
ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” ReactElementλ₯Ό 리턴

import { createElement } from "react";

const GreetingsJSX = () => {
  return <div>Hello, world!</div>;
};

const GreetingsTSX = () => {
  return createElement("div", {
    children: "Hello, world!",
  });
};

PropsWithChildren

import { PropsWithChildren } from "react";

interface Example {
  name: string;
}

const ExampleComponent: FC<PropsWithChildren<Example>> = ({
  name,
  children,
}) => {
  return (
    <div>
      <div>{name}</div>
      {children}
    </div>
  );
};

PropsWithRef

React ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ—μ„œ ref prop을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” ν•¨μˆ˜ = forwardRef()
일반 React.FCμ—μ„œ Refλ₯Ό prop으둜 μ‚¬μš© = PropsWithRef

"ref" extends keyof P ? πŸ‘‰ "ref" λΌλŠ” 속성이 P 의 속성듀 쀑에 ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ κ²€μ‚¬ν•˜λŠ” 쑰건식
P extends { ref?: infer R } ? πŸ‘‰ P κ°€ { ref?: infer R } ν˜•νƒœμΌ λ•Œ, ref의 νƒ€μž…μ„ μΆ”λ‘ ν•˜λŠ” 쑰건식
string extends R ? πŸ‘‰ R 이 string으둜 μΆ”λ‘  κ°€λŠ₯ν•œμ§€ ν™•μΈν•˜λŠ” 쑰건식

import { Ref, RefObject, PropsWithRef, useRef, useEffect } from "react";

type InputProps = {
  inputRef: Ref<HTMLInputElement>;
};

const FormInput: FC<PropsWithRef<InputProps>> = ({ inputRef }) => {
  return <input ref={inputRef} />;
};

RefObject

const RefExample: FC = () => {
  const ref: RefObject<HTMLDivElement> = useRef(null);

  useEffect(() => {
    if (ref.current) {
      ref.current.style.backgroundColor = "red";
    }
  }, []);
  return <div ref={ref}>Hello World</div>;
};

SetStateAction & Dispatch

setState λ©”μ†Œλ“œμ˜ κ²½μš°μ—λŠ” react 라이브러리의 Dispatch, SetStateAction λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©

import { SetStateAction, Dispatch } from "react";

interface ButtonProps {
  setState: Dispatch<SetStateAction<boolean>>;
}

const Button: FC<ButtonProps> = ({ setState }) => {
  return <button onClick={() => setState((prev) => !prev)}>click me!</button>;
};

HTML<>Element

MouseEvent, ChangeEvent, FormEvent, .... (HTMLDivElement, ...etc)

// MouseEvent
const handleClick = (e: MouseEvent<HTMLButtonElement>) => {};
// ChangeEvent
const Form = () => {
  const [inputValue, setInputValue] = useState("");

  const checkValue = (e: ChangeEvent<HTMLInputElement>) => {
    setInputValue(e.target.value);
  };

  return <input type="text" onChange={checkValue} />;
};
// FormEvent
const Todolist = () => {
  const [value, setValue] = useState("");
  const onChange = (e: FormEvent<HTMLInputElement>) => {
    const {
      currentTarget: { value },
    } = e;
    setValue(value);
  };
  const onSubmit = (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    console.log(value);
  };

  return (
    <form onSubmit={onSubmit}>
      <input onChange={onChange} value={value} placeholder="μž…λ ₯ν•˜μ„Έμš”"></input>
      <button>μ œμΆœν•˜κΈ°</button>
    </form>
  );
};



reference.

profile
πŸ₯ž Stack of Thoughts

0개의 λŒ“κΈ€