[React] useEffect์˜ ์‹คํ–‰ ์‹œ์ 
728x90

Today, What I learned?

์ž‘์€ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋‹ค๊ฐ€ null๊ฐ’์ด ํฌํ•จ๋˜์–ด์„œ ์›๋ž˜ ๋˜๋˜ ๊ธฐ๋Šฅ์ด ์•ˆ ๋˜๋Š”(!) ์ผ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.

๋ฐœ๊ฒฌํ•˜์‹  ๋ถ„์ด ๋ฐ”๋กœ ํ•ด๊ฒฐํ•ด ์ฃผ์…จ์ง€๋งŒ, ๋งˆ์นจ ๊ทธ ๋ถ€๋ถ„์ด useEffect๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ถ€๋ถ„์ด๊ณ ..

๋‚ด๊ฐ€ useEffect์˜ ์‹คํ–‰ ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ž˜๋ชป ์•Œ๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์•„ ์ •๋ฆฌํ•œ๋‹ค!

 

 

useEffect์˜ ์‹คํ–‰ ์ˆœ์„œ

๋‚˜๋Š” ๊ธฐ์กด์— useEffect๊ฐ€ ์ œ์ผ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ, (์ •ํ™•ํžˆ ์™œ์˜€๋Š”์ง€๋Š”?..๐Ÿค”)

useEffect ๊ฐ™์€ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๋Š” jsx๊ฐ€ ๋“ค์–ด์žˆ๋Š” return๋ฌธ์ด ๋๋‚˜๊ณ  ์‹คํ–‰๋œ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ˜์†”์„ ์ถ”๊ฐ€ํ•ด์„œ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋‹ค์‹œ ์•Œ๊ฒŒ ๋œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋˜๋Š” return๋ฌธ์ด ๋จผ์ € ์‹คํ–‰๋˜๊ณ  useEffect์˜ ์ฝ˜์†”์ด ์ฐํžˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

const Home = () => {
  console.log('๋ Œ๋”๋ง ์‹œ์ž‘!');

  useEffect(() => {
    console.log('useEffect');
  }, []);

  return (
    <Container>
      {console.log('์ปดํฌ๋„ŒํŠธ ์•ˆ')}
      <Header />
      <TodoForm />
      <TodoList isActive={true} />
      <TodoList isActive={false} />
    </Container>
  );
};

 

 

๋ฆฌ๋ Œ๋”๋ง ๋  ๋•Œ

์ปดํฌ๋„ŒํŠธ์˜ ๋ฆฌ๋ Œ๋”๋ง์€ ์ด๋Ÿฐ ์กฐ๊ฑด์ผ ๋•Œ ๋ฐœ์ƒ๋œ๋‹ค.

  • ์ปดํฌ๋„ŒํŠธ์—์„œ state๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ
  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‚ด๋ ค๋ฐ›์€ props๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋œ ๊ฒฝ์šฐ

 

์•„๋ž˜์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž.

const Test = ({ isActive }: PropsType) => {
  const [text, setText] = useState<string>('');
  const [count, setCount] = useState<Number>(0);

  console.log('text', text);
  console.log('count', count);

  useEffect(() => {
    console.log('useEffect!!');
    setText('Hi');
    setCount(10);
    console.log('useEffect end');
  }, [todos]);

  return (
    <Wrap>
      {console.log('์ปดํฌ๋„ŒํŠธ ์•ˆ')}
      ...

 

์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ˜์†”์ด ์ฐํžˆ๊ฒŒ ๋œ๋‹ค.

์™œ ๋‘ ๋ฒˆ์”ฉ ์ฐํ˜”๋ƒํ•˜๋ฉด ์ž ๊น ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ˆ˜์ •ํ•œ ์ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•œ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‘ ๋ฒˆ ์“ฐ์ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค ๐Ÿ˜‡

๋‘ ๋ฒˆ ์ฐํžŒ ๊ฒƒ์€ ๋ฌด์‹œํ•˜๊ณ ..

์ฝ”๋“œ ์ตœ์ƒ๋‹จ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ฝ˜์†”์„ ์ฐ๊ณ  ์žˆ๋‹ค.

 

์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ๋‹จ ์ฝ”๋“œ -> return ๋ฌธ ๋‚ด๋ถ€ -> useEffect ์ˆœ์„œ๋กœ ์ฐํžˆ๊ณ  ์žˆ๋‹ค.

 

useEffect์˜ ์‹œ์ž‘๊ณผ ๋ ์ฝ˜์†”์ด ์ฐจ๋ก€๋กœ ์ฐํžˆ๊ณ  text์™€ count ์ฝ˜์†”์ด ๋‹ค์‹œ ์ฐํžˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ,

setState๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์— ์‹คํ–‰์ด ๋˜๊ณ ,

ํ•˜๋‚˜์˜ ์Šค์ฝ”ํ”„ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด ํ•œ ๋ฒˆ์— ๊ฐ™์ด ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค. (Batching์ด๋ผ๊ณ  ํ•œ๋‹ค)

 

์ฆ‰ ์œ„์—์„œ๋Š” setText์™€ setCount๊ฐ€ ์—ฐ๋‹ฌ์•„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊บผ๋ฒˆ์— ๋ชจ์•„์„œ state๋ฅผ ๋ณ€๊ฒฝํ•œ ํ›„ ๋ฆฌ๋ Œ๋”๋ง ํ•œ๋‹ค.

๋ฆฌ๋ Œ๋”๋ง์ด ๋˜์—ˆ์œผ๋‹ˆ ๋‹ค์‹œ ์ฝ”๋“œ ์ƒ๋‹จ๋ถ€ํ„ฐ ์ฝ˜์†”์ด ์ฐํžˆ๋Š”๋ฐ, ๋ณ€๊ฒฝ๋œ state๋กœ ์ฐํžˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

์–ด์ œ์˜ ์ฝ”๋“œ๋Š” ๋ฌด์—‡์ด ๋ฌธ์ œ์˜€๋‚˜?

์–ด์ œ ์ž‘์—…ํ–ˆ๋˜ ๊ฒƒ์€ ํ‘ธํ„ฐ์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์„ ํด๋ฆญํ–ˆ์„ ๋•Œ,

ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€์„œ ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ํƒญ์„ ๋ฐ”๋กœ ํ™œ์„ฑํ™” ์‹œ์ผœ์ค˜์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด์—ˆ๋‹ค.

 


์•„๋ž˜๋Š” ๊ธฐ์กด์˜ ์ฝ”๋“œ.

 const { state: categoryFromFooter } = useLocation();

 // ํ‘ธํ„ฐ์—์„œ ํด๋ฆญํ•œ ๊ฒฝ์šฐ ์นดํ…Œ๊ณ ๋ฆฌ ์ง€์ •
  useEffect(() => {
    setCategory(categoryFromFooter);
  }, [categoryFromFooter]);

์ด ์ฝ”๋“œ๋Š” return๋ฌธ ์ดํ›„์— ์‹คํ–‰๋˜๋ฉด์„œ category๋ฅผ ๋‹ค์‹œ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—,
๋งŒ์•ฝ ํ‘ธํ„ฐ์—์„œ ํด๋ฆญํ•ด์„œ ์ด ํŽ˜์ด์ง€๋กœ ๋„˜์–ด์˜จ๊ฒŒ ์•„๋‹ˆ์—ˆ๋‹ค๋ฉด, ๋ฉ€์ฉกํžˆ ์ง€์ •๋˜์–ด ์žˆ๋˜ category๊ฐ€ null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋ฉฐ ๊ธฐ๋Šฅ์ด ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

 

๊ทธ๋ž˜์„œ ์ˆ˜์ •๋œ ๋ถ€๋ถ„์€ ์ด๋ ‡๋‹ค.


ํ‘ธํ„ฐ์—์„œ ์˜จ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ setCategory๋ฅผ ํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.
categoryFromFooter ์ž์ฒด๊ฐ€ ํ‘ธํ„ฐ์—์„œ ์™”์„ ๊ฒฝ์šฐ๋งŒ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํŽ˜์ด์ง€์—์„œ ์˜ฌ ๋•Œ๋Š” ๋ชจ๋‘ null์ด๋‹ค.

 const { state: categoryFromFooter } = useLocation();

 // ํ‘ธํ„ฐ์—์„œ ํด๋ฆญํ•œ ๊ฒฝ์šฐ ์นดํ…Œ๊ณ ๋ฆฌ ์ง€์ •
  useEffect(() => {
    if (categoryFromFooter !== null) {
      setCategory(categoryFromFooter);
    }
  }, [categoryFromFooter]);

 

 

 

 

useEffect์˜ ์‹คํ–‰ ์ˆœ์„œ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋˜์งš์œผ๋ฉฐ ๊ธฐ๋ณธ์— ์ถฉ์‹คํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“  ํ•˜๋ฃจ!...

 

 

728x90