【发布时间】:2020-04-18 14:39:06
【问题描述】:
有什么方法可以在 nextjs getInitialProps 函数中使用或访问 nextRouter/history。
const ViewPost: NextPage<EditPostProps> = (props): React.ReactElement<void> => {
const router = useRouter(); // works here
}
ViewPost.getInitialProps = async (ctx: Context): Promise<EditPostProps> => {
const router = useRouter(); // doesn't work
// ctx props doesn't have history
}
export default withRouter(ViewPost);
我的 api 类接收历史记录或路由器作为参数,因此需要一个,但我无法访问 getInitialProps 中的一个。请问无论如何我可以访问它吗?任何帮助将不胜感激。
【问题讨论】: