React

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

martinooo 2024. 7. 31. 14:20

๋ฆฌ์•กํŠธ ๋ผ์ดํ”„ ์‚ฌ์ดํด

๐Ÿคท‍โ™‚๏ธ Rendering๋ž€?


useLayoutEffect์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๋ฆฌ์•กํŠธ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ Œ๋”๋ง ๊ณผ์ • ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ Œ๋”๋ง(Rendering)์ด๋ž€?

์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์„œ HTML, CSS, JavaScript ๋ฌธ์„œ๋ฅผ ์ฝ๊ณ  ๋ธŒ๋ผ์šฐ์ €์— UI ๊ทธ๋ฆฌ๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

React hooks useSate ์— ๋Œ€ํ•ด์„œ

https://cometruedream.tistory.com/73

 

React hooks useSate ์— ๋Œ€ํ•ด์„œ

๋ฆฌ์•กํŠธ ํ”„๋ ˆ์ž„์›Œํฌ์ค‘์— ๊ผญ์•Œ์•„์•ผํ•  React hooks ์ค‘์— ๊ฐ€์žฅ ์ค‘์š”ํ•œ useState ์ƒํƒœ๊ด€๋ฆฌ ์ž…๋‹ˆ๋‹ค. useState ๊ฐ„๋‹จํ•˜๊ฒŒ ์ปดํฌ๋„ŒํŠธ ์ƒํƒœ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ์—…๋ฐ์ดํŠธ๋ฅผ ์‹œํ‚ฌ์ˆ˜์žˆ๋Š” ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ

cometruedream.tistory.com

 

๐Ÿคท‍โ™‚๏ธ useLayoutEffect๋ž€?


 

 

useLayoutEffect๋Š” useEffect์™€๋Š” ๋™์ž‘๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

 

react hook์ค‘ ํ•˜๋‚˜๋กœ useLayoutEffect๋Š” ๋ Œ๋”๋ง ๋œ ํ›„ UI ์ ์šฉ๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์— useEffect UI ์ ์šฉ๋œ ์ดํ›„์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด ๋ผ์ดํ”„์‚ฌ์ดํด์— ์ฐจ์ด์ ์„ ์ดํ•ดํ•˜๋Š”๊ฒƒ์€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, UI ๊ทธ๋ ค์ง€๊ธฐ ์ „์— ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— DOM์„ ์กฐ์ž‘ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋”๋ผ๋„ ์‚ฌ์šฉ์ž๋Š” ๊นœ๋นก์ž„์„ ๊ฒฝํ—˜ํ•˜์ง€ ์•Š๊ณ  ์ข‹์€ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์˜ UI๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

react hook ๊ณต์‹๋ฌธ์„œ

https://react.dev/reference/react/useLayoutEffect

 

useLayoutEffect – React

The library for web and native user interfaces

react.dev

 

 

 

๐Ÿคท‍โ™‚๏ธ useLayoutEffect์™€ useEffect ์ฐจ์ด์ ?


  • useEffect: ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ™”๋ฉด์ด ๋ Œ๋”๋ง๋œ ํ›„, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ™”๋ฉด์„ ๊ทธ๋ฆฐ ๋‹ค์Œ์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • useLayoutEffect: ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋œ ์งํ›„, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๊ธฐ ์ „์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿคท‍โ™‚๏ธ useLayoutEffect ์‚ฌ์šฉ๋ฒ•


useLayoutEffect hook ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ useEffect์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

useEffect(() => {
  
  // ๋กœ์ง
  return () => /* cleanup */

}, [dependancy array]);
    
useLayoutEffect(() => {

  // ๋กœ์ง
  return () => /* cleanup */
  
}, [dependancy array]);

์ฃผ์˜์ 

๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด, useLayoutEffect๋Š” ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚ฌ ์žˆ์–ด์„œ useEffect๋ฅผ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

๊ผญ ํ•„์š”ํ•œ ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚ค๋Š” useLayoutEffect๋ณด๋‹ค useEffect๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

 

*์„œ๋ฒ„์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์ธ ๊ฒฝ์šฐ์—๋Š” useLayoutEffect์™€ useEffect ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!

 

๐Ÿคท‍โ™‚๏ธ useLayoutEffect ์‚ฌ์šฉ์˜ˆ์ œ


๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์‹ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import React, {  useLayoutEffect, useRef, useState } from "react";

function App() {
  const [height, setHeight] = useState(123);
  const divRef = useRef();

  useLayoutEffect(() => {
    // div ์š”์†Œ์˜ ๋†’์ด๋ฅผ ์ธก์ •ํ•˜์—ฌ ์ƒํƒœ์— ์ €์žฅ
    if (divRef.current) {
      setHeight(divRef.current.getBoundingClientRect().height);
    }
  }, []); // ๋นˆ ๋ฐฐ์—ด์„ ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

  return (
    <div>
      <div ref={divRef} style={{ padding: "20px", border: "1px solid black" }}>
        ์ธก์ •ํ•  div ์š”์†Œ์ž…๋‹ˆ๋‹ค.
      </div>
      <p>์ด div ์š”์†Œ์˜ ๋†’์ด๋Š” {height}px ์ž…๋‹ˆ๋‹ค.</p>
    </div>
  );
}

export default App;

 

์œ„์— ์ฝ”๋“œ๋ฅผ ์„ค๋ช…

  1. useRef๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ div ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  2. useLayoutEffect๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ UI๋ฅผ ๋ Œ๋”๋ง ํ•˜๊ธฐ ์ „์— useRef๋ฅผ ์ฐธ์กฐํ•œ div ๋†’์ด๋ฅผ ์ธก์ •ํ•˜๊ณ  ์ƒํƒœ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  3. height์— div์š”์†Œ์— ๋†’์ด ๊ฐ’์„ ํ™”๋ฉด์—์„œ ๋ณด์‹ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„์— ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ๊ฒƒ์€ useLayoutEffect hook ์–ธ์ œ ์ˆ˜ํ–‰ํ–ˆ๋Š๋ƒ? ๋ธŒ๋ผ์šฐ์ €์— UI ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๊ธฐ ์ „์— ํ˜ธ์ถœ๋˜์–ด์„œ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿคท‍โ™‚๏ธ ๋งˆ์ง€๋ง‰์œผ๋กœ


๋ฆฌ์•กํŠธ์— useLayoutEffect ํ›…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

 

๋ธŒ๋ผ์šฐ์ € UI๊ฐ€ ๊ทธ๋ ค์ง€๊ธฐ์ „์— ๊นœ๋ฐ•์ž„ ์—†์ด, ๋น ๋ฅด๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ DOM์กฐ์ž‘์ด ํ•„์š”ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์‹ค ์ˆ˜ ์žˆ๋Š” ํ›…์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ useLayoutEffect ์„ฑ๋Šฅ์ €ํ•˜์˜ ์›์ธ์ด ๋ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜์‹œ๊ณ  useEffect hook์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 


 

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

 

 

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