亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Nextjs - 將道具從自定義 _app.js 傳遞到 <Navbar> 組件?

Nextjs - 將道具從自定義 _app.js 傳遞到 <Navbar> 組件?

繁星點點滴滴 2023-10-14 19:27:37
如何將道具從這里傳遞到要添加到 Head 組件下的導航欄組件?我已經使用 getServerSideProps 在 index.js 頁面中完成了此操作,但它似乎在 _app.js 文件中不起作用。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,};有沒有更好的方法在每個頁面中使用導航欄組件,或者這是正確的方法嗎?
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

當這是我最大的問題時的美好時光哈哈

對于真正需要答案的人:

您當前無法在 _app 組件中使用 getStaticProps 和 getServerSideProps ,因此,如果我沒記錯的話,我會嘗試獲取初始數據(可能是類別列表)并將其傳遞給組件。要在 _app 級別獲取初始數據,您應該使用 getInitialProps(NextJs 開發人員建議避免使用它),但請注意,使用它會阻止 NextJs 自動靜態優化不依賴于任何類型獲取方法的頁面(它們將在服務器端呈現 - 即使是一個簡單的“關于我們”頁面,也可以作為靜態 html 提供)

查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號