登录后重定向网站路径,而不是主页reactjs



从电子邮件到用户订单列表页面的路径,如果未登录,将转到登录页面。登录后如何将页面转到订单列表页面?当前登录后,页面将转到主页

我认为您可以使用goBack()方法。

import { useHistory } from 'react-router-dom'
const history = useHistory()

并且在登录之后

history.goBack();

希望这能帮助您解决问题

所以对于登录按钮做这个

<button>
<Link
to={{
pathname: '/login',  // this pathname should be equal to the route that you have defined for Login page
state: { from: 'cart' },
}}
>
Login to see order list
</Link>
</button>

因此,上面的代码片段应该转到检查用户是否登录的页面,如果用户最初没有登录,则将其重定向到登录页面。

现在,在您的Login.js组件文件中useEffect钩下

useEffect(() => {
if (user is logged in condition) {
let intended = history.location.state; // this history object comes react-router-dom
if (intended) {
history.push(intended.from);
} 
}
}, []);

最新更新