React

๋ฆฌ์•กํŠธ ์ œ์–ด vs ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ์•Œ์•„๋ณด๊ธฐ

martinooo 2024. 8. 2. 16:33

๋ฆฌ์•กํŠธ

๐Ÿคท‍โ™‚๏ธ ๋ฆฌ์•กํŠธ ํผ ์š”์†Œ๋ž€?


๋ฆฌ์•กํŠธ์—์„œ ํผ(form) ์š”์†Œ์— ์ƒํƒœ๊ฐ’์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ(controlled component)์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ(uncontrolled component) ๋ฐฉ์‹์œผ๋กœ ๋‚˜๋ˆ ์ง‘๋‹ˆ๋‹ค.

 

์ด ๋‘ ๊ฐ€์ง€ ์ ‘๊ทผ ๋ฐฉ์‹์—” ์žฅ๋‹จ์ ์ด ์žˆ์œผ๋ฉฐ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์„ ํƒํ•ด์„œ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

2024.07.31 - [React] - ๋ฆฌ์•กํŠธ useLayoutEffect ๊ฐœ๋… ๋ฐ ์‚ฌ์šฉ๋ฒ•

 

๋ฆฌ์•กํŠธ useLayoutEffect ๊ฐœ๋…๋ฐ ์‚ฌ์šฉ๋ฒ•

๐Ÿคท‍โ™‚๏ธ Rendering๋ž€?useLayoutEffect์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๋ฆฌ์•กํŠธ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ Œ๋”๋ง ๊ณผ์ • ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ Œ๋”๋ง(Rendering)์ด๋ž€?์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์„œ HTML, CSS, JavaScript ๋ฌธ์„œ๋ฅผ ์ฝ๊ณ  ๋ธŒ๋ผ

cometruedream.tistory.com

 

๐Ÿคท‍โ™‚๏ธ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ?


 

 

์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋ฐฉ์‹์€ HTML์—์„œ input, textarea, select์™€ ๊ฐ™์€ ํผ(form) ์—˜๋ฆฌ๋จผํŠธ๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ state(์ƒํƒœ)๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

 

ํผ(form) ์—˜๋ฆฌ๋จผํŠธ์—์„œ ์ž…๋ ฅ๋˜๋Š” ์ƒํƒœ๊ฐ’์€ state๋กœ ๊ด€๋ฆฌ, ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, ๋ฆฌ์•กํŠธ์— ์˜ํ•ด ๊ฐ’์ด ์ œ์–ด๋˜๋Š” ์ž…๋ ฅํผ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ์ด๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.

 

๋ฆฌ์•กํŠธ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๊ณต์‹๋ฌธ์„œ

https://ko.legacy.reactjs.org/docs/forms.html#controlled-components

 

ํผ – React

A JavaScript library for building user interfaces

ko.legacy.reactjs.org

 

์ œ์–ด ์ปดํฌ๋„ŒํŠธ ์˜ˆ์ œ

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [inputValue, setInputValue] = useState("");

  const handleChange = (event) => {
    setInputValue(event.target.value);
  };

  const handleSubmit = (event) => {
    event.preventDefault();
    alert(`Submitted value: ${inputValue}`);
  };

  console.log("inputValue::", inputValue);

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Controlled Input:
        <input type="text" value={inputValue} onChange={handleChange} />
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

 

useState hook์œผ๋กœ input ์ƒํƒœ๊ฐ’์„ ์ œ์–ดํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

์ œ์–ด ์ปดํฌ๋„ŒํŠธ input

์‚ฌ์šฉ์ž๊ฐ€ input ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ œ์–ด ์ปดํฌ๋„ŒํŠธ

์‹ค์‹œ๊ฐ„ ๋™๊ธฐํ™”๋˜๋ฉฐ input state ๊ฐ’์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿคท‍โ™‚๏ธ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ??


๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋ฐฉ์‹์€ ํผ(form)์˜ ๋ฐ์ดํ„ฐ๊ฐ€ state ์ƒํƒœ ๊ฐ’์ด ์•„๋‹Œ ๋ฐ”๋‹๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฉ์‹์ธ DOM ์š”์†Œ์— ์˜ํ•ด ์ œ์–ด๋˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋Š” ref๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ input, select, textarea ๊ฐ’์„ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ์˜ˆ์ œ

import React, { useRef } from "react";
import "./styles.css";

export default function App() {
  const inputRef = useRef(null);

  const handleSubmit = (event) => {
    event.preventDefault();
    console.log(`Submitted value: ${inputRef.current.value}`);
  };

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Uncontrolled Input:
        <input type="text" ref={inputRef} />
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

 

useRef hook ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์–ป์–ด์˜ค๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

 

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ input

์‚ฌ์šฉ์ž๊ฐ€ input ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ

 

์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋‹ค๋ฅด๊ฒŒ ์ƒํƒœ ๊ฐ’์„ ์‹ค์‹œ๊ฐ„ ๋™๊ธฐํ™” ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ž…๋ ฅ๊ฐ’์„ ์–ป์–ด์˜ต๋‹ˆ๋‹ค.

 

๐Ÿคท‍โ™‚๏ธ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ vs ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ?


  • ์ œ์–ด ์ปดํฌ๋„ŒํŠธ:
    • ์žฅ์ : ํผ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ปดํฌ๋„ŒํŠธ์˜ state์— ์ €์žฅ๋˜๋ฏ€๋กœ, ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ, ์ตœ์‹  ๊ฐ’์„ ์œ ์ง€ํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค..
    • ๋‹จ์ : ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ , state๋ฅผ ์„ค์ •ํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ:
    • ์žฅ์ : ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ , ์ž…๋ ฅ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ๋” ์ง๊ด€์ ์ด๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ ์‹œ์— ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜์ง€ ์•Š์•„ ์„ฑ๋Šฅ์— ์ข‹์Šต๋‹ˆ๋‹ค.
    • ๋‹จ์ : ์ž…๋ ฅ ๊ฐ’์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฒ€์ฆํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐ’์€ ํผ์„ ์ œ์ถœํ•  ๋•Œ๋งŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋””๋ฐ”์šด์‹ฑ & ์“ฐ๋กœํ‹€๋ง ์•Œ์•„๋ณด๊ธฐ!

https://cometruedream.tistory.com/222

 

ํ”„๋ก ํŠธ์—”๋“œ ๊ผญ ํ•„์š”ํ•œ ๋””๋ฐ”์šด์‹ฑ๊ณผ ์“ฐ๋กœํ‹€๋ง ์ค‘์š” ๊ฐœ๋…

๋””๋ฐ”์šด์Šค์™€ ์“ฐ๋กœํ‹€๋ง ๋ชจ๋‘ ์›น์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์Šคํฌ๋กค ์ด๋ฒคํŠธ์˜ ๊ฒฝ์šฐ ์Šคํฌ๋กค๋ง ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ๊ทธ ๋•Œ๋งˆ๋‹ค ๊ฐ™์€ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€

cometruedream.tistory.com

 

๐Ÿคท‍โ™‚๏ธ ์ œ์–ด vs ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋งˆ์ง€๋ง‰์œผ๋กœ


๋ฆฌ์•กํŠธ์— ์ œ์–ด ์ปดํฌ๋„ŒํŠธ์™€ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ์— ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

 

ํผ ์š”์†Œ์— ์ƒํƒœ๊ฐ’์„ ๋‹ค๋ฃจ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์„œ๋น„์Šค์— ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ œ์–ด or ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์‹œ๋ฉด ๋ ๊บผ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์ €๋Š” ์ž…๋ ฅ ๊ฐ’์„ ์‹ค์‹œ๊ฐ„ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ํ•˜๊ฑฐ๋‚˜ ์ž…๋ ฅ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์—, ๊ฐ„๋‹จํ•œ ํผ์„ ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ ์ž…๋ ฅ ๊ฐ’์— ๋”ฐ๋ผ ๊ฒ€์ฆ์ด ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ ๋น„์ œ์–ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 


 

์žฌ๋ฐŒ๋Š” ์˜ค๋Š˜์˜ ๋ ๋ณ„ ์šด์„ธ ๋ณด๊ณ  ๊ฐ€์„ธ์š”! 

 

 

https://cometruedream.tistory.com/247

 

[React] Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผ className ์ปค์Šคํ…€

๐Ÿคท‍โ™‚๏ธ Tailwind ํ•œ์ค„, ๋‘์ค„, ์„ธ์ค„ ํšจ๊ณผํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘ ๋ฐฐ๋„ˆ ๋งํฌ์— URL ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ 3์ค„ ํšจ๊ณผ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. React + Tailwind CSS ์…‹ํŒ…๋œ ํ”„๋กœ์ ํŠธ ์—ฌ์„œ CSS ํŒŒ์ผ & style ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/243

 

NestJS Lifecycle (์ƒ๋ช…์ฃผ๊ธฐ) ๊ฐœ๋…

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œ Nest JS ๊ฐœ๋…?Nest JS๋Š” Node JS ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ƒ๋‹นํžˆ ์˜ค๋žซ๋™์•ˆ Express ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋ฒ„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์—์„œ ์••๋„์ ์ธ ์ 

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/242

 

๋ฆฌ์•กํŠธ Vite Proxy ํ™œ์šฉํ•œ CORS ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋ก ํŠธ์—”๋“œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ต์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ๊ธ€์„ ๋ณด์‹œ๋ฉด ๋” ๋งŽ์€ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค^^ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋ฒˆ๋“ค๋Ÿฌ ๊ฐœ๋…๊ณผ webpack vs vite ๋น„๊ตhttps://comet

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/241

 

React ํด๋”๊ตฌ์กฐ FSD ๊ธฐ๋Šฅ ๋ถ„ํ™œ ์„ค๊ณ„ ์•„ํ‚คํ…์ฒ˜

๐Ÿคท‍โ™‚๏ธ ๋ณธ๋ก ์— ์•ž์„œํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ํด๋” ๊ตฌ์กฐ, ์„ค๊ณ„์— ๋Œ€ํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ์–ด๋ ต๋‹ค. ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์— React FSD ํด๋”๊ตฌ์กฐ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ค๊ณ„ํ•  ์˜ˆ์ •์ด๋ผ์„œ FSD ์•„ํ‚คํ…

cometruedream.tistory.com

 

 

https://cometruedream.tistory.com/244

 

์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋Š” Prisma ๋ช…๋ น์–ด ์ •๋ฆฌ

๐Ÿคท‍โ™‚๏ธ Prisma ๋ช…๋ น์–ด ๋ณธ๋ก ์— ์•ž์„œ Prisma๋ž€?Nest JS ํ”„๋ ˆ์ž„์›Œํฌ์™€ Prisma ORM์„ ํ™œ์šฉํ•ด์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ Prisma๋Š” Node JS ๊ธฐ๋ฐ˜์— ํ”„๋ ˆ์ž„์›Œํฌ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ƒํ˜ธ์ž‘์šฉ์ด ์ž˜๋˜๋ฉฐ Prisma + DB ์—ฐ๋™์œผ

cometruedream.tistory.com

 

 

https://fnfentermagazine.com/%ec%9e%90%eb%b0%94%ec%8a%a4%ed%81%ac%eb%a6%bd%ed%8a%b8-%ec%88%ab%ec%9e%90-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0%ec%9d%98-%ea%b0%9c%eb%85%90%ea%b3%bc-%eb%82%b4%ec%9e%a5%ed%95%a8%ec%88%98/

 

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ 2ํƒ„] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ˆซ์ž ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐœ๋…๊ณผ ๋‚ด์žฅํ•จ์ˆ˜

์„ธ์ƒ์„ ํ–ฅํ•ด ๋‹ฌ๋ ค - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(javascript) ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์ˆ˜ํ•™์  ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ 2ํƒ„] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ˆซ์ž ์ž๋ฃŒ๊ตฌ์กฐ์˜

fnfentermagazine.com