Front-End/์—๋Ÿฌ์‚ฌํ•ญ

ESLint: Expected to return a value at the end of arrow function.(consistent-return)

์™•๊ฐ€๐Ÿ‘ 2023. 7. 27. 15:24
728x90
๋ฐ˜์‘ํ˜•

ESLint์— ์˜ํ•ด ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์ž…๋‹ˆ๋‹ค.

 

 


 

 

๋ฌธ์ œ

useEffect(() => {
  if (!response) {
    return setLoading(false);
  }

  api();
  ...
}

useEffect ํ›…์—์„œ return setState๋ฅผ ์คฌ๋”๋‹ˆ ํ•จ์ˆ˜ ์ž์ฒด์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

๊ณต์‹ ๋ฌธ์„œ ์ฐพ์•„๋ณด๋‹ˆ, ํ•ด๋‹น ๋ฌธ์ œ์˜ ๋ฐœ์ƒ์›์ธ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค

 

  • return ๋ฌธ์€ ์ข…๋ฃŒํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (it does not execute a return statement before it exits)
  • ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์€ return๋ฌธ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (it executes return which does not specify a value explicitly)
  • ์ •์˜๋˜์ง€ ์•Š์€ return์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (it executes return undefined)
  • return void ๋’ค์— ์‹์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (it executes return void followed by an expression (for example, a function call))
  • return ๋’ค์— ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ํ‰๊ฐ€๋˜๋Š” ๋‹ค๋ฅธ ์‹์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
    (
    it executes return followed by any other expression which evaluates to undefined)

๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋Œ๋ฆฐ ๊ฑฐ๋ผ ์ •ํ™•ํ•˜๊ฒŒ ํ•ด์„๋˜์ง€๋Š” ์•Š์ง€๋งŒ, ์•„๋งˆ ์œ„ ์ƒํ™ฉ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” 4๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

useEffect์•ˆ์— callback์œผ๋กœ ๋ฐ›์€ ํ•จ์ˆ˜๋Š” void์ธ๋ฐ, return ํ•˜๋ฉด์„œ setState๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ ๋•Œ๋ฌธ์— ESLint์—์„œ ์—๋Ÿฌ๋กœ ์žก์€ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.

 

 


 

 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ

useEffect(() => {
  if (!response) {
    setLoading(false);
    return;
  }

  api();
  ...
}

 

๊ฐ„๋‹จํ•˜๊ฒŒ  setState๋ฅผ ์‹คํ–‰ํ•œ ๋’ค, retrun ์‹œํ‚ด์œผ๋กœ์จ useEffect๋ฅผ ์ข…๋ฃŒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

728x90