✏️ Next.js

[Next.js] Custom App Component

category
✏️ Next.js
date
thumbnail
slug
next-js-Custom-App-Component
author
status
Public
tags
summary
type
Post
 
NextJS는 pages 폴더에서 _app.js 파일을 먼저 찾고, 그 다음에 index.js를 찾는다.
즉, 모든 페이지에서 공통으로 사용할 코드가 있다면 _app.js 파일을 생성 후 그곳에 작성하면 된다.
 
_app.js 에서 사용하는 컴포넌트는 2개의 파라미터를 받는다. ComponentpageProps
 
  • Component : 렌더링하길 원하는 페이지
  • pageProps : 넘겨줄 props
 

ex) 모든 페이지에서 공통으로 사용하는 NavBar 컴포넌트

pages/_app.js
import NavBar from '@/components/NavBar';

export default function App({ Component, pageProps }) {
  return (
    <>
      <NavBar />
      <Component {...pageProps} />
    </>
  );
}
 

styles/globals.css 파일은 Custom App 에서만 import 가능

다른 컴포넌트 파일에서 styles/globals.css 파일을 사용하면 에러가 난다.
styles/globals.css 파일은 Custom App 인 _app.js 에서 불러서 사용하자.
 

참고