起生 發表於 2022-5-24 17:14:00

[Next.js] Override the Default App Component in Next.js

<p>You can override the default App component in Next.js by creating a&nbsp;<code>_app.tsx</code>&nbsp;file and defining your own&nbsp;<code>App</code>&nbsp;component. By doing this, you can use global styles, pass custom props, and more.</p>
<p>pages/_app.tsx</p>
<pre class="language-javascript"><code>import type {AppProps} from "next/app";

const App = ({Component, pageProps}: AppProps) =&gt; {
    // whatever you pass to pageProps object, will be passed to Children component as well
    pageProps.title = "This is override Home page title";
    return &lt;Component {...pageProps} /&gt;
}
   
export default App;</code></pre>
<p>&nbsp;</p>
<p>pages/Home.tsx</p>
<pre class="language-javascript"><code>const Home ({title}: {title: string}) =&gt; {
    return (
      &lt;div&gt;
            &lt;!-- will receive title from _app.js --&gt;
            &lt;h1&gt;{title}&lt;/h1&gt;
      &lt;/div&gt;
    )
}

export default Home;</code></pre>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/16306287.html
頁: [1]
查看完整版本: [Next.js] Override the Default App Component in Next.js