简体   繁体   English

Next.js - 页面未将 GetServerSideProps 放入页面组件道具中(使用自定义 _app.js)

[英]Next.js - Page not getting GetServerSideProps into Page component props (with custom _app.js)

I've been trying to add GetServerSideProps to my Next.js app, but when trying to do so, the response data is not being injected into the page props.我一直在尝试将GetServerSideProps添加到我的 Next.js 应用程序中,但是在尝试这样做时,响应数据没有被注入到页面道具中。 When looking into the Network Tab, i see the pageName.json generating, so the call is being executed and the data being fetched, it just doesn't get into the Page as Props.当查看网络选项卡时,我看到pageName.json生成,因此正在执行调用并获取数据,它只是没有作为道具进入页面。

Here's my page component.ts这是我的页面 component.ts

import { useState } from "react";
import { url } from "../app/helpers/api/apiEnvs";
import { Button, Spin, Skeleton } from "antd";
import { MailOutlined, LoadingOutlined } from '@ant-design/icons';
import withLayout from "../app/components/Layout";
import './quotes.scss'

export const getServerSideProps = async (ctx) => {
const res = await fetch(`${url}/estimator/start`,
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
        });
    const estimator = await res.json();
    return {
        props: { estimator }
    };
};


const Quotes = (props: any) => {

    const [loading, setLoading] = useState(false);
    const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;

    return (
        <div className="quotesContainer">
            <h1>Cotizador</h1>
            <div className="quoteBox quoteStepper">
            <MailOutlined/>
            </div>
            <div className="quoteBox quoteContent">
            {loading
                ? <Spin indicator={antIcon} />
            : <div>
                    <Skeleton.Input style={{ width: '300px' }} active={true} size="large" />
                    <p>{props.estimation ? props.estimation: 'No estimation'}</p>

                    <Skeleton.Button style={{ width: '90px' }} active={true} size="default" />
                    <Button type="primary">
                        Next
                    </Button>
              </div>
            }
            </div>
        </div>
    );
};

export default withLayout(Quotes);

Custom _app.js Component自定义 _app.js 组件

import 'antd/dist/antd.css';

export default function App({ Component, pageProps }) {
    return <Component {...pageProps} />
}

screenshot of the data being displayed in the network tab网络选项卡中显示的数据的屏幕截图

could it be related to having ts pages and .js _app?它可能与拥有 ts 页面和 .js _app 相关吗?

It looks like the loaded props wont passed through your withLayout-wrapper.看起来加载的道具不会通过你的 withLayout-wrapper。 Can u post the content of your withLayout-wrapper?你可以发布你的 withLayout-wrapper 的内容吗?

I had the same problem with the withRedux-wrapper from next-redux-wrapper.我对 next-redux-wrapper 的 withRedux-wrapper 也有同样的问题。 Inside the wrapper should be a static async getInitialProps function which do the same like the default getInitialProps from next itself:包装器内部应该是一个静态异步 getInitialProps 函数,它的作用与 next 本身的默认 getInitialProps 相同:

async function appGetInitialProps({ Component, ctx}:AppContext): Promise<AppInitialProps> {
    const pageProps = await loadGetInitialProps(Component, ctx)
    return { pageProps }
}

The loadGetInitialProps function comes with next and can be be imported from next/dist/next-server/lib/utils loadGetInitialProps 函数是 next 自带的,可以从 next/dist/next-server/lib/utils 导入

Try change _app.js like this尝试像这样更改 _app.js

  static async getInitialProps(appContext) {

    const appProps = await App.getInitialProps(appContext);

    if (!Object.keys(appProps.pageProps).length) {
      delete appProps.pageProps;
    }

    return appProps;
  }

If the return of _app getInitialProps is empty object.如果_app getInitialProps 返回的是空对象。 NextJs will use the props returned from getServerSideProps if available如果可用,NextJs 将使用从 getServerSideProps 返回的道具

There might be custom _app.js in which you must be calling getInitialProps .可能有自定义_app.js ,您必须在其中调用getInitialProps there some modification is required需要一些修改

static async getInitialProps({ Component, ctx }) {
     const pageProps = Component.getInitialProps ? await Component.getInitialProps(ctx) : {};

    if (Object.keys(pageProps).length > 0) {
      // return pageProps only when its present
      return { pageProps }; 
    
    }
    return {};
  }

yes, you have to return pageProps only when it is present.是的,只有在pageProps时才必须返回它。

So, the issue was with the wrapper,所以,问题出在包装纸上,

Note: Use Layout components (which in my case is in the hole app) inside _app.js注意:在 _app.js 中使用 Layout 组件(在我的例子中是在hole app 中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Next.js,在页面组件中传递getServerSideProps和children props - Next.js , pass getServerSideProps and children props in the page component Next.js 在 getServerSideProps 方法中访问 Page props - Next.js access Page props inside the getServerSideProps method React 组件道具未在 Next.js 页面内呈现 - React component props not getting rendered inside Next.js Page 在Next.JS 6中使用_app.js页面组件时,转换触发得太早了 - Transitions are firing too early when using _app.js page component in Next.JS 6 Next.JS typescript 通过 getServerSideProps 将 props 返回给外部组件 - Next.JS typescript pass getServerSideProps returned props to external component Next.js 和 Typescript - 如何在功能组件中访问 app.js 的 props 和 staticProps - Next.js with Typescript - how to access app.js props and staticProps in functional component Next.js 使用 getServerSideProps 如何将道具从页面传递到组件? - Next.js using getServerSideProps how do i pass props from page to components? 在页面加载时将道具传递给子组件 - 与 next.js 反应 - Pass props to a child component on page load - react with next.js 自定义 Next.js App 组件中未继承 React 道具 - React props are not inherited in Custom Next.js App component 等待_app.js加载,然后在Next.js上运行组件 - Wait _app.js loaded then run component on Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM