【发布时间】:2021-07-03 14:35:08
【问题描述】:
我在 NextJS 中有以下 HOC。
我希望通过withRouter HOC 访问 NextJS 路由器,获取如下类组件。
import UserManager from "../managers/user_manager";
import LogInPage from "../pages/auth/login";
import React from "react";
import { withRouter } from 'next/router'
export default function EnsureAuthenticated(OriginalComponent) {
class AuthHOC extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.loggedInUser = UserManager.getLoggedInUser();
}
render() {
if (!this.loggedInUser) {
this.props.router.push(LogInPage.routePath);
}
return <OriginalComponent />
}
}
return withRouter(AuthHOC);
}
但这是我不断收到的错误:
Server Error
Error: No router instance found. you should only use "next/router" inside the client side of your app. https://nextjs.org/docs/messages/no-router-instance
我有什么不同的方法来解决这个问题? 谢谢。
【问题讨论】: