在 getStaticProps
方法的末尾,
export const getStaticProps: GetStaticProps = async ({ ...context }) => {
我向组件传递参数:
return {
props: {
eventId,
buyTicketDataBE: buyTicketDataBE
? buyTicketDataBE
: "Error, no event found",
sortedBookings: sortedBookings ?? null,
recommendations: recommendations ?? null,
sortedEventTimes: sortedEventTimes ?? null,
couponRedeemString: couponRedeemString ?? null,
translations: translations.default,
lang: context.params?.lang ?? fallBackLang,
},
revalidate: buyTicketDataBE.timeElapsedSinceLastSale ?? 600,
};
但在组件中,并不总是接收。
interface BuyTicketProps {
eventId: string;
buyTicketDataBE: BuyTicketData;
sortedBookings: SortedBookings;
recommendations: { [position: string]: Recommendation };
sortedEventTimes: EventTimes;
couponRedeemString: string;
translations: Translation;
lang: string;
}
function BuyTicket(props: BuyTicketProps) {
const {
eventId,
buyTicketDataBE,
sortedBookings,
recommendations,
sortedEventTimes,
couponRedeemString,
translations,
lang,
} = props;
const [buyTicketData, setBuyTicketData] = useState({ ...buyTicketDataBE });
console.log("eventId 1", eventId);
console.log("org id 1", buyTicketData.organizationId);
这里最后两个 lise 被调用了两次,输出如下:
eventId 1 undefined
org id 1 undefined
eventId 1 undefined
org id 1 undefined
eventId 1 128228399715137
org id 1 undefined
奇怪,因为当我在终端中检查服务器渲染时,我可以看到两个值都是从 BE 收到的。那为什么不传给客户端呢?
eventId 1 128228399715137
org id 1 1AFEFE82-865B-47F7-8DD0-FEE5704B323D
或者客户端不会得到所有这些参数?