使用React重定向到另一条路线

时间:2020-08-07 08:28:39

标签: reactjs

当我注销用户时,

window.location.reload();

返回登录页面,但我想知道是否有更好的方法来处理此问题?

const handleLogout = () => {
   logout();
   window.location.reload();
};

1 个答案:

答案 0 :(得分:0)

来自react-router-dom的

useHistory钩子将完成此工作:

const history = useHistory()

history.push("/login")

此处示例https://codesandbox.io/s/elated-kare-op595?file=/src/Home.js