【发布时间】:2021-06-26 16:47:36
【问题描述】:
我正在学习如何将 Next.js/React 用于我的应用程序。我目前正在研究路由的主题,我有几个问题。我知道你可以将 react-router 用于 React(我之前使用过 vue-router)。但是,我不知道是否需要将 react-router 与 Next.js 一起使用,以及如果可以的话我将如何使用它。我目前正在使用页面目录来保存要重定向到的页面。如何在 React/Next 中重定向到不同的页面?
下面是一些实现它的示例代码:
Class Login extends Component {
state = {
user: {},
}
loginUser = (e) => {
loginUser(this.state.user)
.then(response => {
console.log(response);
if (response['username']) {
console.log('yippee!');
}
});
}
}
在 yippee 之后,我想重定向到 pages 文件夹中的 /home。
【问题讨论】:
-
我建议您阅读
next/routerdocumentation。特别是withRouter部分。
标签: javascript reactjs routes react-router next.js