๐Ÿ’ป ๋น„๋“œ๋งˆ์ผ“ ๋ฆฌํŒฉํ† ๋ง - ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์ ์šฉ

waterglassesยท2022๋…„ 9์›” 23์ผ
1

๋ฆฌํŒฉํ† ๋ง

๋ชฉ๋ก ๋ณด๊ธฐ
5/6
post-thumbnail
post-custom-banner

โš ๏ธ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์€ ์˜คํƒ€๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์ฒ˜๋ฆฌ

SSR๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์˜ฌ ๊ฒฝ์šฐ ๋กœ๋”ฉ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ

์ƒํ’ˆ ์นด๋“œ๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ getServerSideProps๋กœ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ๊ฐ€์ ธ์˜ค๊ธฐ ์ „๊นŒ์ง€ ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๊ฐ€ ์—†์–ด์„œ ํ™”๋ฉด์ด ๋ฉˆ์ถฐ์žˆ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๋“ค์ด ์ดํƒˆํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋„ˆ๋ฌด ๋†’๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ์„ ํ•  ํ•„์š”์„ฑ์„ ๋Š๊ผˆ๋‹ค.

Router.events

์œ„์™€ ๊ฐ™์€ ๊ฒƒ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด next/router์—์„œ ์ œ๊ณตํ•˜๋Š” router event์ด๋‹ค.
router event๋Š” ๋‹จ์–ด ๊ทธ๋Œ€๋กœ ๋ผ์šฐํŠธ ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํŠธ๋ฆฌ๊ฑฐ ๋˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค.

router.events

  • routeChangeStart(url, { shallow }) - ๋ผ์šฐํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ธฐ ์‹œ์ž‘ํ• ๋•Œ ํŠธ๋ฆฌ๊ฑฐ ๋ฉ๋‹ˆ๋‹ค
  • routeChangeComplete(url, { shallow }) - ๋ผ์šฐํŠธ๊ฐ€ ์™„์ „ํžˆ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํŠธ๋ฆฌ๊ฑฐ ๋ฉ๋‹ˆ๋‹ค
  • routeChangeError(err, url, { shallow }) - ๋ผ์šฐํŠธ ๋ณ€๊ฒฝ ์ค‘์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ฑฐ๋‚˜, ์ทจ์†Œ๋˜์—ˆ์„ ๋•Œ ํŠธ๋ฆฌ๊ฑฐ ๋ฉ๋‹ˆ๋‹ค

์œ„์˜ ์„ธ๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ์ด ๋•Œ useEffect ํ›…์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ์ฒซ ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์‹œ์— ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.

page/_app.js์— Router.events ์ ์šฉ

๋˜ํ•œ page/_app.js์— ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ–ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ชจ๋“  ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค SSR๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋”๋ผ๋„ ๋กœ๋”ฉ์ด ์ ์šฉ ๋˜์–ด ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ๊ฐœ์„ ๋œ๋‹ค.

๊ฒฐ๊ตญ, ํŠน์ •ํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ํŽ˜์ด์ง€์— ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ž‘์„ฑํ•˜๊ธฐ๋ณด๋‹ค app.js์— ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฝ๋„๋ก ํ•˜์˜€๋‹ค.

์ฝ”๋“œ

import { Router } from 'next/router';
import { useEffect, useState } from 'react';

function MyApp({ Component, pageProps }: AppProps) {
  const [isLoading, setIsLoading] = useState(false);

  useEffect(() => {
    const startLoading = () => {
      setIsLoading(true);
    };

    const finishedLoading = () => {
      setIsLoading(false);
    };

    Router.events.on('routeChangeStart', startLoading);
    Router.events.on('routeChangeComplete', finishedLoading);
    Router.events.on('routeChangeError', finishedLoading);

    return () => {
      Router.events.off('routeChangeStart', startLoading);
      Router.events.off('routeChangeComplete', finishedLoading);
      Router.events.off('routeChangeError', finishedLoading);
    };
  }, []);

  return (
    <Layout>
    	{isLoading ? <Loading /> : <Component {...pageProps} />}
    </Layout>
  );
}

export default MyApp;

๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋กœ ๊ฐœ์„ 

แ„‰แ…กแ†ผแ„‰แ…ฆแ„‘แ…ฆแ„‹แ…ตแ„Œแ…ตแ„…แ…ฉแ„ƒแ…ตแ†ผ

๐Ÿ”ฅ ๋Š๋‚€์ 

next/router์— ํŽ˜์ด์ง€ ์ด๋™ํ•˜๋Š” ๊ธฐ๋Šฅ ๋ง๊ณ ๋„ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋•๋ถ„์— ๋กœ๋”ฉ์ฒ˜๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ _app.js์—์„œ ๋กœ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ตœ์„ ์˜ ๋ฐฉ๋ฒ•์ธ์ง€๋Š” ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒ ๋‹ค.

Refer

Nextjs ๊ณต์‹๋ฌธ์„œ next/router - router events

profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€