[英]NextJS getInitialProps is not passing how I would expect and I get compiler error "varName not found on type {}"
我想我有一个简单的语法问题,现在在 NextJs 上阻碍了我。
我正在尝试做一些动态服务器端提取,所以我尝试了 getInitialProps 模式,但编译器无法识别正常功能组件渲染中 getInitialProps 的返回:
interface Props {
date: string,
user: number
}
const DayTimeView: NextComponentType<Props> = ({timeRecords, date, user}) => {
return (
<div>
<h1>DayTimeView</h1>
</div>
)
}
DayTimeView.getInitialProps = async (props: Props) => {
let sesh = getSession();
let timeRecords = []
getSession().then((session) => {fetch('http://localhost:3000/api/time/date/' +new Date(props.date).toISOString(), {
method: 'GET',
headers: { "Content-Type": "application/json",
"x-auth-token": session.user.id},
})
.then(res => res.json().then(data => {
if (data.length > 0) {
timeRecords = data;
}else{
timeRecords = []; // Todo default
}
}))})
return {props: { timeRecords: timeRecords, date: props.date, user: props.user }}
}
export default DayTimeView;
这并不是 timeRecords 不存在于道具中的事实,因为即使在删除 timeRecords 时对于日期和用户的相同错误它也不起作用。
DayTimeView
应该是NextPage
类型,它是一个NextComponentType
,它还包含一个 NextJS 上下文。
import { NextPage } from 'next';
const DayTimeView: NextPage<Props> = ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.