查看: 157|回复: 0

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

[复制链接]

4

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-12-5
发表于 2022-5-24 17:14:00 | 显示全部楼层 |阅读模式

You can override the default App component in Next.js by creating a _app.tsx file and defining your own App component. By doing this, you can use global styles, pass custom props, and more.

pages/_app.tsx

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;

 

pages/Home.tsx

const Home ({title}: {title: string}) => {
    return (
        <div>
            <!-- will receive title from _app.js -->
            <h1>{title}</h1>
        </div>
    ) 
}

export default Home;

 



来源:https://www.cnblogs.com/Answer1215/p/16306287.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部