[英]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.