νμ
μ€ν¬λ¦½νΈμλ κ΅μ₯ν λ§μ νμ
λ€μ΄ μ‘΄μ¬νκ³ μ€μ λ‘λ μμ£Ό μ¬μ©λμ§ μλ νμ
λ€λ λ§μ΄ μ‘΄μ¬ν©λλ€.
μλλ λ§μ κ°λ°μλ€μ΄ μ€λ¬΄μμ μ¬μ©νλ©΄μ κ°νΉ νΉμ μμ£Ό μ¬μ©νλ νμ
λ€ μ€ λ¦¬μ‘νΈ νμ
λ€μ
λλ€.
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μμ μ¬μ©λλ νμ
μ€ν¬λ¦½νΈ μ©μ΄λ‘, "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>
);
};
children μμ±μ νμ
μΌλ‘ κ°μ₯ λ§μ΄ μ¬μ©νλ νμ
π jsx λ΄μμ μ¬μ©ν μ μλ λͺ¨λ μμμ νμ
(string, null, undefined λ±μ ν¬ν¨νλ κ°μ₯ λμ λ²μλ₯Ό κ°λ νμ
)
μ΄λ€ propsμ λ°μ 건λ°, ꡬ체μ μΌλ‘ μ΄λ€ νμ
μ΄ μ¬μ§ μ μ μκ±°λ, μ΄λ ν νμ
λ λͺ¨λ λ°κ³ μΆλ€λ©΄ ReactNodeλ‘ μ§μ ν΄μ£Όλ κ²μ΄ μ’μ΅λλ€.
ν΄λμ€ν μ»΄ν¬λνΈλ renderλ©μλμμ ReactNodeλ₯Ό 리ν΄
createElement ν¨μλ₯Ό ν΅ν΄ μμ±λ μ»΄ν¬λνΈλ§μ νμ©νλ νμ
ν¨μν μ»΄ν¬λνΈλ ReactElementλ₯Ό 리ν΄
import { createElement } from "react";
const GreetingsJSX = () => {
return <div>Hello, world!</div>;
};
const GreetingsTSX = () => {
return createElement("div", {
children: "Hello, world!",
});
};
import { PropsWithChildren } from "react";
interface Example {
name: string;
}
const ExampleComponent: FC<PropsWithChildren<Example>> = ({
name,
children,
}) => {
return (
<div>
<div>{name}</div>
{children}
</div>
);
};
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} />;
};
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>;
};
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>;
};
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.