Front-End/React

React] Hook : useEffect

์™•๊ฐ€๐Ÿ‘ 2023. 6. 20. 10:08
728x90
๋ฐ˜์‘ํ˜•

useEffect

Side-Effect๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ Hook์ž…๋‹ˆ๋‹ค.

Side-Effect : ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ํ•จ์ˆ˜ ์™ธ๋ถ€์— ์กด์žฌํ•˜๋Š” ๊ฐ’์ด๋‚˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ๋“ฑ์˜ ํ–‰์œ„๋กœ, ๋ Œ๋”๋ง ๋œ ์ดํ›„ ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š” ๋ถ€์ˆ˜์ ์ธ ๊ฒƒ๋“ค์„ ์ง€์นญ

 

uesEffect(() ⇒ { … } ,[deps])

 

  • ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ deps์— state๊ฐ’์„ ๋„ฃ๊ณ , state๊ฐ’์ด ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น useEffect๋ฅผ ์‹คํ–‰.
  • ๋นˆ ๋ฐฐ์—ด๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ, ๋ Œ๋”๋ง ํ›„ 1๋ฒˆ๋งŒ ์‹คํ–‰.
  • ์ข…์†์„ฑ ๋ฐฐ์—ด์„ ์•„์˜ˆ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰.

 

useEffect๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์€ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ๊ฐ€ DOM์— ๋ฐ˜์˜๋  ๋•Œ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฌ๋ผ์ง€๊ธฐ ์ง์ „ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

2023.06.20 - [Front-End/React] - React ๋™์ž‘ Mount & Rerender

 

(๋งˆ์šดํŠธ ๋œ ๋‹ค์Œ ํ˜ธ์ถœ๋˜๋Š”๋ฐ, ๋งˆ์šดํŠธ์™€ ๋ Œ๋”๋ง์€ ๋‹ค์Œ ๊ฒŒ์‹œ๊ธ€์—์„œ ์„ค๋ช…)

 

 


 

์˜ˆ

 

deps๋ฅผ ๋นˆ๋ฐฐ์—ด๋กœ ๋„ฃ์—ˆ์„ ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜์กด์„ฑ ๋ฐฐ์—ด์— ๋‹ค๋ฅธ state๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€๋„ ํ…Œ์ŠคํŠธํ•ด๋ณผ ์ˆ˜ ์žˆ๊ณ , ๋นˆ๋ฐฐ์—ด ๋˜๋Š” ์ธ์ž๊ฐ’ ์ž์ฒด๋ฅผ ์•ˆ ๋„ฃ์—ˆ์„ ๋•Œ, ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค useEffect๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


 

 

์ฃผ์˜์‚ฌํ•ญ

  • useEffect ์‚ฌ์šฉ ์‹œ, props๋ฅผ deps์— ๋„ฃ์–ด์ฃผ๊ณ , ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ state๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด, ๋ฆฌ๋ Œ๋”๋ง์ด ๋งŽ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์ด๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
function TodoList({ todos, filter }) {
  const [newTodo, setNewTodo] = useState('');

  // ๐Ÿ”ด Avoid: redundant state and unnecessary Effect
  const [visibleTodos, setVisibleTodos] = useState([]);
  
  useEffect(() => {
    setVisibleTodos(getFilteredTodos(todos, filter));
  }, [todos, filter]);

  // ...
}

 

์œ„์™€ ๊ฐ™์ด useEffect์— todos, filter๋ฅผ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ, props๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋  ๊ฒƒ์ด๊ณ ,

๊ทธ๋Ÿฌ๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ธ Todolist๋Š” ๋ณธ์ธ ์˜์ง€์™€ ์ƒ๊ด€์—†์ด ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ฆฌ๋ Œ๋”๋ง ๋˜๊ณ , useEffect๊ฐ€ ์‹คํ–‰๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ถ”๊ฐ€๋กœ useEffect์—์„œ state๊ฐ€ ๋ฐ”๋€๊ฒƒ์„ ์ธ์ง€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ํ•œ๋ฒˆ ๋” ๋ฆฌ๋ Œ๋”๋ง ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์ด ์ž์‹์ปดํฌ๋„ŒํŠธ์—์„œ ๋งŽ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.

function TodoList({ todos, filter }) {
  const [newTodo, setNewTodo] = useState('');
  
  // โœ… This is fine if getFilteredTodos() is not slow.
  const visibleTodos = getFilteredTodos(todos, filter);
  
  // useMemo๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ ํ™” ๊ฐ€๋Šฅ
  const visibleTodos = useMemo(() => getFilteredTodos(todos, filter), [todos, filter]);
  // ...
}

 

visibleTodo๊ฐ€ state๊ฐ’์—์„œ ์ง€์—ญ๋ณ€์ˆ˜๋กœ ๋ฐ”๋€Œ์—ˆ๋Š”๋ฐ, ์–ด์ฐจํ”ผ ํ™”๋ฉด์ด ๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค getFilteredTodos๊ฐ€ ์‹คํ–‰๋  ๊ฒƒ์ด๊ณ , ์ œ์ผ ์ตœ์‹  ๋ฐ์ดํ„ฐ๋กœ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๊ฒŒ ์„ฑ๋Šฅ์ƒ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด, useMemo๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ todos๋‚˜ filter๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

(useMemo๋Š” ๋‹ค์Œ ๊ฒŒ์‹œ๊ธ€์—...)

 

  • ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
export default function ProfilePage({ userId }) {
  const [comment, setComment] = useState('');

  // ๐Ÿ”ด Avoid: Resetting state on prop change in an Effect
  useEffect(() => {
    setComment('');
  }, [userId]);
  // ...
}

 

๋ฌธ์ œ์ ์€ ์œ„์™€ ๋น„์Šทํ•˜๊ฒŒ userId์— ๋”ฐ๋ผ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

export default function ProfilePage({ userId }) {
  return (
    <Profile
      userId={userId} 
      key={userId}
    />
  );
}

function Profile({ userId }) {
  // โœ… This and any other state below will reset on key change automatically
  const [comment, setComment] = useState('');
  // ...
}

 

Profile์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚ด๋ถ€์— ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ comment๋ฅผ ์ดˆ๊ธฐํ™”์‹œ์ผœ์คŒ์œผ๋กœ์จ, ๋ฆฌ๋ Œ๋”๋ง์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(userId์— ๋”ฐ๋ผ Profile ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ...)

 

 


 

 

useEffect๋Š” ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” hook ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์„œ๋Š” useEffect๋ž‘ useState๋งŒ ์ž˜ ์‚ฌ์šฉํ•ด๋„, ๊ธฐ๋ณธ์ ์ธ ํŽ˜์ด์ง€๋“ค์€ ์ถฉ๋ถ„ํžˆ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฑฐ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.
728x90

'Front-End > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

React] Strict Mode  (0) 2023.06.20
React] Mount & Rerender (LifeSycle)  (0) 2023.06.20
React] Hook  (0) 2023.06.20
React] Hook : useState  (1) 2023.06.06
React] Hook : props  (1) 2023.06.06