Javascript Nextjs-将道具从custom _app.js传递到<;导航栏>;组件?
如何将道具从这里传递到要添加到头部组件下的导航栏组件? 我已经在index.js页面中使用getServerSideProps完成了,但在app.js文件中似乎不起作用Javascript Nextjs-将道具从custom _app.js传递到<;导航栏>;组件?,javascript,reactjs,next.js,react-props,Javascript,Reactjs,Next.js,React Props,如何将道具从这里传递到要添加到头部组件下的导航栏组件? 我已经在index.js页面中使用getServerSideProps完成了,但在app.js文件中似乎不起作用 import "../_app.css"; import React from "react"; import PropTypes from "prop-types"; import Head from "next/head"; import {
import "../_app.css";
import React from "react";
import PropTypes from "prop-types";
import Head from "next/head";
import { ThemeProvider } from "@material-ui/core/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
import theme from "../theme";
export default function MyApp({ Component, pageProps }) {
React.useEffect(() => {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector("#jss-server-side");
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
}, []);
return (
<React.Fragment>
<Head>
<title>Furnibnz | Baldai Internetu ir Nemokamas Pristatymas</title>
<meta
name="viewport"
content="minimum-scale=1, initial-scale=1, width=device-width"
/>
</Head>
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</React.Fragment>
);
}
MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object.isRequired,
};
导入“./_app.css”;
从“React”导入React;
从“道具类型”导入道具类型;
从“下一个/Head”导入Head;
从“@material ui/core/styles”导入{ThemeProvider}”;
从“@material ui/core/CssBaseline”导入CssBaseline;
从“./主题”导入主题;
导出默认函数MyApp({Component,pageProps}){
React.useffect(()=>{
//删除服务器端注入的CSS。
const jssStyles=document.querySelector(“jss服务器端”);
if(jssStyles){
jssStyles.parentElement.removeChild(jssStyles);
}
}, []);
返回(
Furnibnz | Baldai Internetu ir Nemokamas Pristatymas
{/*CssBaseline启动了一个优雅、一致且简单的基线来构建。*/}
);
}
MyApp.propTypes={
组件:PropTypes.elementType.isRequired,
pageProps:PropTypes.object.isRequired,
};
是否有更好的方法在每页中使用导航栏组件,或者这是正确的方法?这是我最大的问题时的好时光lol 对于真正需要答案的人: 您当前无法在_app组件中使用getStaticProps和getServerSideProps,因此如果我没有错的话,我尝试获取初始数据(可能是类别列表)并将其传递给组件。 要在应用程序级别获取初始数据,您应该使用getInitialProps(NextJs开发人员建议避免使用它),但请注意,使用它将阻止NextJs自动静态优化不依赖任何获取方法的页面(它们将在服务器端呈现-甚至是简单的“关于我们”页面)可以用作静态html的页面(否则) 您可以在此处阅读更多信息: