[Next.js] Override the Default App Component in Next.js
<p>You can override the default App component in Next.js by creating a <code>_app.tsx</code> file and defining your own <code>App</code> 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) => {
// whatever you pass to pageProps object, will be passed to Children component as well
pageProps.title = "This is override Home page title";
return <Component {...pageProps} />
}
export default App;</code></pre>
<p> </p>
<p>pages/Home.tsx</p>
<pre class="language-javascript"><code>const Home ({title}: {title: string}) => {
return (
<div>
<!-- will receive title from _app.js -->
<h1>{title}</h1>
</div>
)
}
export default Home;</code></pre>
<p> </p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/16306287.html
頁:
[1]